如何摆脱MVCAttribute路由引起的错误?

时间:2017-02-13 17:39:09

标签: c# asp.net-mvc asp.net-mvc-routing attributerouting

我在常规路由的同时启用了MVC属性路由。每次运行应用程序时都会出现此错误。

  

类型的内联约束解析器   ' DefaultInlineConstraintResolver'无法解决以下问题   内联约束:' string'。描述:未处理的异常   在执行当前Web请求期间发生。请   查看堆栈跟踪以获取有关错误和位置的更多信息   它起源于代码。

     

异常详细信息:System.InvalidOperationException:内联   类型' DefaultInlineConstraintResolver'的约束解析器是   无法解决以下内联约束:' string'。

这是堆栈跟踪:

  

[InvalidOperationException:类型的内联约束解析器   ' DefaultInlineConstraintResolver'无法解决以下问题   内联约束:' string'。]
  System.Web.Mvc.Routing.InlineRouteTemplateParser.GetInlineConstraint(集团   constraintGroup,Boolean isOptional,IInlineConstraintResolver   constraintResolver)+389
  System.Web.Mvc.Routing.InlineRouteTemplateParser.ParseRouteTemplate(字符串   routeTemplate,IDictionary 2 defaults, IDictionary 2个约束,   IInlineConstraintResolver constraintResolver)+488
  System.Web.Mvc.Routing.DirectRouteFactoryContext.CreateBuilder(字符串   template,IInlineConstraintResolver constraintResolver)+308
  System.Web.Mvc.Routing.DirectRouteFactoryContext.CreateBuilderInternal(字符串   模板)+48
  System.Web.Mvc.Routing.DirectRouteFactoryContext.CreateBuilder(字符串   模板)+44
  System.Web.Mvc.RouteAttribute.System.Web.Mvc.Routing.IDirectRouteFactory.CreateRoute(DirectRouteFactoryContext   上下文)+80
  System.Web.Mvc.Routing.DefaultDirectRouteProvider.CreateRouteEntry(字符串   areaPrefix,String controllerPrefix,IDirectRouteFactory factory,   IReadOnlyCollection 1 actions, IInlineConstraintResolver constraintResolver, Boolean targetIsAction) +115
System.Web.Mvc.Routing.DefaultDirectRouteProvider.CreateRouteEntries(String areaPrefix, String controllerPrefix, IReadOnlyCollection
1个工厂,   IReadOnlyCollection 1 actions, IInlineConstraintResolver constraintResolver, Boolean targetIsAction) +155
System.Web.Mvc.Routing.DefaultDirectRouteProvider.GetActionDirectRoutes(ActionDescriptor actionDescriptor, IReadOnlyList
1个工厂,IInlineConstraintResolver   constraintResolver)+188
  System.Web.Mvc.Routing.DefaultDirectRouteProvider.GetDirectRoutes(ControllerDescriptor   controllerDescriptor,IReadOnlyList 1 actionDescriptors, IInlineConstraintResolver constraintResolver) +245
System.Web.Mvc.Routing.AttributeRoutingMapper.AddRouteEntries(SubRouteCollection collector, IEnumerable
1个controllerTypes,IInlineConstraintResolver   constraintResolver,IDirectRouteProvider directRouteProvider)+234
  System.Web.Mvc.Routing.AttributeRoutingMapper.MapAttributeRoutes(RouteCollection   routes,IEnumerable`1 controllerTypes,IInlineConstraintResolver   constraintResolver,IDirectRouteProvider directRouteProvider)+333
  System.Web.Mvc.Routing.AttributeRoutingMapper.MapAttributeRoutes(RouteCollection   routes,IInlineConstraintResolver constraintResolver,   IDirectRouteProvider directRouteProvider)+398
  System.Web.Mvc.Routing.AttributeRoutingMapper.MapAttributeRoutes(RouteCollection   routes,IInlineConstraintResolver constraintResolver)+192
  System.Web.Mvc.RouteCollectionAttributeRoutingExtensions.MapMvcAttributeRoutes(RouteCollection   路线)+123
  SocialManager.RouteConfig.RegisterRoutes(RouteCollection路由)中   c:\ Users \ Naser Dostdar \ Documents \ Visual Studio   2013 \项目\ SocialManager \ SocialManager \ App_Start \ RouteConfig.cs:16   C:\ Users \ Naser中的SocialManager.MvcApplication.Application_Start()   Dostdar \ Documents \ Visual Studio   2013 \项目\ SocialManager \ SocialManager \ Global.asax.cs中:18

     

[HttpException(0x80004005):类型的内联约束解析器   ' DefaultInlineConstraintResolver'无法解决以下问题   内联约束:' string'。]
  System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext的   上下文,HttpApplication app)+9942821
  System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr的   appContext,HttpContext上下文,MethodInfo []处理程序)+118
  System.Web.HttpApplication.InitSpecial(HttpApplicationState状态,   MethodInfo [] handlers,IntPtr appContext,HttpContext context)+172
  System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr的   appContext,HttpContext context)+352
  System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr的   appContext)+296

     

[HttpException(0x80004005):类型的内联约束解析器   ' DefaultInlineConstraintResolver'无法解决以下问题   内联约束:' string'。]
  System.Web.HttpRuntime.FirstRequestInit(HttpContext context)+9924184   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context)   +101 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest)   wr,HttpContext context)+261

以下是我的Route.Config文件的样子:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapMvcAttributeRoutes();

        routes.MapRoute(null, "Page{page}",
        new
        {
            controller = "Blogs",
            action = "Index",
            category =
                (string)null
        },
        new { page = @"\d+" }
        );
        routes.MapRoute(null,
        "{category}",
        new { controller = "Blogs", action = "Edit", page = 1 }
        );
        routes.MapRoute(null,
        "{category}/Page{page}",
        new { controller = "Blogs", action = "List" },
        new { page = @"\d+" }
        );
        routes.MapRoute(null, "{controller}/{action}");


        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

P.S:我还没有在我的项目中定义任何MVC属性路由,只是想测试启用MVC属性路由功能。

Web API版本:2.2

1 个答案:

答案 0 :(得分:2)

某处您已定义了属性路由。没有其他任何东西会导致此错误。你已经完成了这项工作,你已经使用string作为路线约束。例如:

[Route("{foo:string}")]

但是,string不是有效的路由约束,因为路径中的所有都是字符串。长和短,找到您定义的包含:string的路径属性并删除它。