如何省略首页看法控制器名称 [英] How to omit controller name for Home views
问题描述
让我们说我有这样的结构:
指数
关于项目/
指数
细节
我怎么可以省略控制器名称为家庭的看法?
我想写 {根} /关于
而不是 {根} /首页/关于
。
我也想 {}根/项目/细节/ 2
工作
下面是我在尝试的RegisterRoutes
:
routes.IgnoreRoute({}资源个.axd / {*} PATHINFO);routes.MapRoute(
名称:默认路由
网址:{控制器} / {行动} / {ID}
默认:新
{
控制器=家,
行动=索引,
ID = UrlParameter.Optional
}
);routes.MapRoute(
名称:HomeRoute
网址:{}行动,
默认:新
{
控制器=家,
行动=索引
}
);
编辑:我也试过换我的图路线
调用顺序,但它仍然无法正常工作结果
我需要的是:
{}根/首页/索引> HomeController.Index
{}根/家庭> HomeController.Index
{}根与GT; HomeController.Index
{}根/主页/关于> HomeController.About
{}根/关于> HomeController.About
{}根/项目/索引> ProjectController.Index
{}根/项目> ProjectController.Index
{}根/项目/细节/ 12> ProjectController.Details
只需更改顺序的图路线
调用:
routes.MapRoute(
名称:HomeRoute
网址:{}行动,
默认:新{控制器=家,行动=索引}
);routes.MapRoute(
名称:默认,
网址:{控制器} / {行动} / {ID}
默认:新{控制器=家,行动=索引,ID = UrlParameter.Optional}
);
在默认路线已被最后定义,否则它匹配所有的URL模式并没有进一步的路线进行评估。
更新:
根据您的编辑,因为你要preserve的控制器名称只是路线为好。试试这个:
公共类ActionExistsConstraint:IRouteConstraint
{
私人只读类型_controllerType; 公共ActionExistsConstraint(类型controllerType)
{
this._controllerType = controllerType;
} 公共BOOL匹配(HttpContextBase HttpContext的,路由路径,字符串参数名称,RouteValueDictionary价值,RouteDirection routeDirection)
{
VAR actionName =值[行动]作为字符串; 如果(actionName == NULL || _controllerType == NULL || _controllerType.GetMethod(actionName,BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.IgnoreCase)== NULL)
返回false; 返回true;
}
}
然后:
routes.MapRoute(
名称:HomeRoute
网址:{}行动,
默认:新{控制器=家,行动=索引},
限制:新{存在=新ActionExistsConstraint(typeof运算(HomeController中))}
);
请参阅 MSDN
Let's say I have this structure:
Home/
Index
About
Project/
Index
Details
How can I omit the controller name for Home views?
I want to write {root}/About
instead of {root}/Home/About
.
I also want {root}/Project/Details/2
to work.
Here is what I tried in RegisterRoutes
:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "DefaultRoute",
url: "{controller}/{action}/{id}",
defaults: new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
}
);
routes.MapRoute(
name: "HomeRoute",
url: "{action}",
defaults: new
{
controller = "Home",
action = "Index"
}
);
Edit: I also tried swapping the order of my MapRoute
calls but it still doesn't work.
What I need is:
{root}/Home/Index > HomeController.Index
{root}/Home > HomeController.Index
{root} > HomeController.Index
{root}/Home/About > HomeController.About
{root}/About > HomeController.About
{root}/Project/Index > ProjectController.Index
{root}/Project > ProjectController.Index
{root}/Project/Details/12 > ProjectController.Details
Just change the order of your MapRoute
calls:
routes.MapRoute(
name: "HomeRoute",
url: "{action}",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The 'Default' route has to be defined last, otherwise it matches all Url patterns and no further routes are evaluated.
Update:
As per your edit, since you want to preserve the 'controller-name-only' route as well. Try this:
public class ActionExistsConstraint : IRouteConstraint
{
private readonly Type _controllerType;
public ActionExistsConstraint(Type controllerType)
{
this._controllerType = controllerType;
}
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
var actionName = values["action"] as string;
if (actionName == null || _controllerType == null || _controllerType.GetMethod(actionName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.IgnoreCase) == null)
return false;
return true;
}
}
Then:
routes.MapRoute(
name: "HomeRoute",
url: "{action}",
defaults: new { controller = "Home", action = "Index" },
constraints: new { exists = new ActionExistsConstraint(typeof(HomeController)) }
);
See MSDN
这篇关于如何省略首页看法控制器名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!