首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UrlHelper.RouteUrl()抛出SecurityException

UrlHelper.RouteUrl()抛出SecurityException
EN

Stack Overflow用户
提问于 2018-02-17 21:22:53
回答 1查看 48关注 0票数 0

我正在尝试在ASP.NET的Windows主机上托管一个新的GoDaddy站点。

在构建站点导航时,代码使用System.Web.Mvc.UrlHelper中的RouteUrl方法为每个页面动态生成正确的URL。

在本地,这工作得很好。然而,在GoDaddy的服务器上,显示了这个错误:

代码语言:javascript
复制
[SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
   System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) +0
   System.Security.CodeAccessSecurityEngine.Check(CodeAccessPermission cap, StackCrawlMark& stackMark) +34
   System.Security.CodeAccessPermission.Demand() +46
   System.Web.HttpContext.System.IServiceProvider.GetService(Type service) +54
   System.Web.HttpContextWrapper.GetService(Type serviceType) +13
   System.Web.WebPages.UrlRewriterHelper.IsUrlRewriterTurnedOn(HttpContextBase httpContext) +108
   System.Web.WebPages.UrlRewriterHelper.WasRequestRewritten(HttpContextBase httpContext) +13
   System.Web.WebPages.UrlUtil.GenerateClientUrlInternal(HttpContextBase httpContext, String contentPath) +138
   System.Web.WebPages.UrlUtil.GenerateClientUrl(HttpContextBase httpContext, String contentPath) +97
   System.Web.Mvc.UrlHelper.GenerateUrl(String routeName, String actionName, String controllerName, RouteValueDictionary routeValues, RouteCollection routeCollection, RequestContext requestContext, Boolean includeImplicitMvcValues) +139
   System.Web.Mvc.UrlHelper.GenerateUrl(String routeName, String actionName, String controllerName, String protocol, String hostName, String fragment, RouteValueDictionary routeValues, RouteCollection routeCollection, RequestContext requestContext, Boolean includeImplicitMvcValues) +35
   System.Web.Mvc.UrlHelper.RouteUrl(String routeName, Object routeValues, String protocol) +48
   System.Web.Mvc.UrlHelper.RouteUrl(String routeName, Object routeValues) +15
   SITE_NAME_REDACTED.Web.Classes.Navigation.checkPageType(PageModel page, String parentSlug) in Navigation.cs:54
   SITE_NAME_REDACTED.Web.Classes.Navigation.loopNavigation(List`1 navigation, String parentSlug) in Navigation.cs:35
   SITE_NAME_REDACTED.Web.Classes.Navigation.GetNavigation() in Navigation.cs:24
   SITE_NAME_REDACTED.Web.Controllers.BaseController.prepareViewModel(String menu, String subMenu, Action`1 action) in BaseController.cs:34
   SITE_NAME_REDACTED.Web.Controllers.HomeController.Index(String menu, String subMenu, String gallery) in HomeController.cs:13

课程内容如下所示。checkPageType()中的page.Url = _urlHelper.RouteUrl("Default", routeValues);行是抛出错误的地方。

代码语言:javascript
复制
using SITE_NAME_REDACTED.Data;
using SITE_NAME_REDACTED.Data.Models;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Routing;

namespace SITE_NAME_REDACTED.Web.Classes
{
    public class Navigation
    {
        private IDataRepository _repo;
        private UrlHelper _urlHelper;

        public Navigation(IDataRepository repo, RequestContext context)
        {
            _repo = repo;
            _urlHelper = new UrlHelper(context);
        }

        public List<PageModel> GetNavigation()
        {
            List<PageModel> navigation = _repo.GetNavigation();

            return loopNavigation(navigation);
        }

        private List<PageModel> loopNavigation(List<PageModel> navigation, string parentSlug = "")
        {
            List<PageModel> newNav = new List<PageModel>();

            foreach (PageModel page in navigation)
            {
                if (page.Active)
                {
                    checkPageType(page, parentSlug);
                    newNav.Add(page);
                }
            }

            return newNav;
        }

        private void checkPageType(PageModel page, string parentSlug)
        {
            object routeValues;

            if (page.Type == "Collection")
            {
                page.Children = loopNavigation(page.Children, page.Slug);
            }
            else if (page.Type != "Link")
            {
                routeValues = getRouteValues(parentSlug, page.Slug);
                page.Url = _urlHelper.RouteUrl("Default", routeValues);
                page.LinkTarget = "_top";
            }
            else
            {
                page.LinkTarget = "_blank";
            }
        }

        private object getRouteValues(string parentSlug, string pageSlug)
        {
            object result;

            if (string.IsNullOrEmpty(parentSlug))
            {
                result = new
                {
                    menu = pageSlug
                };
            }
            else
            {
                result = new
                {
                    menu = parentSlug,
                    subMenu = pageSlug,
                    gallery = ""
                };
            }

            return result;
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2018-02-17 21:38:46

经过更多的研究,我在this page on GoDaddy's help site的帮助下找到了它。

基本上,调用UriHelper.RouteUrl会触发CAS信任级别检查。GoDaddy默认为中等信任级别,但此检查需要完全信任。按照链接文章中的以下步骤解决了问题:

更改您的信任级别的

登录到您的Hosting.

  • Next帐户。单击您想要使用的主机帐户的Web Hosting.

  • Next,单击您想要使用的域名主机部分的底部,单击显示More.

  • Click
  1. GoDaddy信任级别菜单,选择您想要的设置,然后点击OK.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48841786

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档