Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用于从区域中删除控制器名称的自定义路由_C#_Asp.net Mvc_Routes_Asp.net Mvc Routing - Fatal编程技术网

C# 用于从区域中删除控制器名称的自定义路由

C# 用于从区域中删除控制器名称的自定义路由,c#,asp.net-mvc,routes,asp.net-mvc-routing,C#,Asp.net Mvc,Routes,Asp.net Mvc Routing,我想从区域中的URL中删除控制器名称 我试试这个 context.MapRoute("AreasName_default", "AreasName/{action}/{id}", new {action = "Index", id = UrlParameter.Optional }); 但它得到了一个错误: 匹配的路由不包括“控制器”路由值,该值 是必需的” 尝试对区域使用属性路由,而不是调用MapRoute(): 要启用属性路由

我想从区域中的URL中删除控制器名称

我试试这个

 context.MapRoute("AreasName_default", "AreasName/{action}/{id}", new {action = "Index", id = UrlParameter.Optional });
但它得到了一个错误:

匹配的路由不包括“控制器”路由值,该值 是必需的”


尝试对区域使用属性路由,而不是调用MapRoute():

要启用属性路由,必须添加对routes.mapmvCatributeRoutes()方法的调用,并在RouteConfig.cs文件的RegisterRoutes()方法中使用:

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

    // Enabling attribute routing. Recommend to put attribute routes in the route table before traditional routes.
    routes.MapMvcAttributeRoutes();

    // Additional calls
    routes.MapRoute(...)
}

干得好!将这一行添加到RouteConfig.cs之后:“AreaRegistration.Registeralareas();”@nawaz uddin:当我创建一个新项目时,Visual Studio模板已经包括
AreaRegistration.Registeralareas()
作为
应用程序启动()方法中的第一行。
public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    // Enabling attribute routing. Recommend to put attribute routes in the route table before traditional routes.
    routes.MapMvcAttributeRoutes();

    // Additional calls
    routes.MapRoute(...)
}