UrlHelper.RouteUrl()抛出SecurityException

时间:2018-02-17 13:22:53

标签: c# asp.net-mvc iis

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

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

在当地,这很好用。但是,在GoDaddy的服务器上,显示了以下错误:

[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

课程内容如下。 page.Url = _urlHelper.RouteUrl("Default", routeValues);中的checkPageType()行是引发错误的位置。

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;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

经过一番研究后,我能够在this page on GoDaddy's help site的帮助下追踪它。

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

  

改变您的信任等级

     
      
  1. 登录您的GoDaddy帐户。
  2.   
  3. 点击虚拟主机。
  4.   
  5. 在您要使用的主机帐户旁边,点击管理。
  6.   
  7. 在您要使用的域名托管部分的底部,点击显示更多。
  8.   
  9. 单击 ASP.NET设置。
  10.   
  11. CAS信任级别菜单中,选择所需的设置,然后点击确定。
  12.