如何摆脱由 MVC 属性路由引起的这个错误?

How do I get rid of this error caused by MVCAttribute routing?

我在约定路由的同时启用了 MVC 属性路由。我每次 运行 应用程序时都会收到此错误。

The inline constraint resolver of type 'DefaultInlineConstraintResolver' was unable to resolve the following inline constraint: 'string'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The inline constraint resolver of type 'DefaultInlineConstraintResolver' was unable to resolve the following inline constraint: 'string'.

这是堆栈跟踪:

[InvalidOperationException: The inline constraint resolver of type 'DefaultInlineConstraintResolver' was unable to resolve the following inline constraint: 'string'.]
System.Web.Mvc.Routing.InlineRouteTemplateParser.GetInlineConstraint(Group constraintGroup, Boolean isOptional, IInlineConstraintResolver constraintResolver) +389
System.Web.Mvc.Routing.InlineRouteTemplateParser.ParseRouteTemplate(String routeTemplate, IDictionary2 defaults, IDictionary2 constraints, IInlineConstraintResolver constraintResolver) +488
System.Web.Mvc.Routing.DirectRouteFactoryContext.CreateBuilder(String template, IInlineConstraintResolver constraintResolver) +308
System.Web.Mvc.Routing.DirectRouteFactoryContext.CreateBuilderInternal(String template) +48
System.Web.Mvc.Routing.DirectRouteFactoryContext.CreateBuilder(String template) +44
System.Web.Mvc.RouteAttribute.System.Web.Mvc.Routing.IDirectRouteFactory.CreateRoute(DirectRouteFactoryContext context) +80
System.Web.Mvc.Routing.DefaultDirectRouteProvider.CreateRouteEntry(String areaPrefix, String controllerPrefix, IDirectRouteFactory factory, IReadOnlyCollection1 actions, IInlineConstraintResolver constraintResolver, Boolean targetIsAction) +115<br> System.Web.Mvc.Routing.DefaultDirectRouteProvider.CreateRouteEntries(String areaPrefix, String controllerPrefix, IReadOnlyCollection1 factories, IReadOnlyCollection1 actions, IInlineConstraintResolver constraintResolver, Boolean targetIsAction) +155<br> System.Web.Mvc.Routing.DefaultDirectRouteProvider.GetActionDirectRoutes(ActionDescriptor actionDescriptor, IReadOnlyList1 factories, IInlineConstraintResolver constraintResolver) +188
System.Web.Mvc.Routing.DefaultDirectRouteProvider.GetDirectRoutes(ControllerDescriptor controllerDescriptor, IReadOnlyList1 actionDescriptors, IInlineConstraintResolver constraintResolver) +245<br> System.Web.Mvc.Routing.AttributeRoutingMapper.AddRouteEntries(SubRouteCollection collector, IEnumerable1 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 routes) +123
SocialManager.RouteConfig.RegisterRoutes(RouteCollection routes) in c:\Users\Naser Dostdar\Documents\Visual Studio 2013\Projects\SocialManager\SocialManager\App_Start\RouteConfig.cs:16 SocialManager.MvcApplication.Application_Start() in c:\Users\Naser Dostdar\Documents\Visual Studio 2013\Projects\SocialManager\SocialManager\Global.asax.cs:18

[HttpException (0x80004005): The inline constraint resolver of type 'DefaultInlineConstraintResolver' was unable to resolve the following inline constraint: 'string'.]
System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +9942821
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +118
System.Web.HttpApplication.InitSpecial(HttpApplicationState state, 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): The inline constraint resolver of type 'DefaultInlineConstraintResolver' was unable to resolve the following inline constraint: '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

某处 你已经定义了一个属性路由。没有其他东西会导致此错误。在你这样做的地方,你已经使用 string 作为路由约束。例如:

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

但是,string 不是有效的路由约束,因为路由中的一切 都是字符串。总之,找到您定义的包含 :string 的路由属性并将其删除。