我想根据组的值制作控制器,所以我有以下路线:
routes.MapRoute(
name: "Default",
url: "{group}/{action}",
defaults: new { group = "Default", action = "Index" }
).RouteHandler = new MyRouteHandler();
使用以下 routehandler 和 httphandler:
public class MyRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return new MyHttpHandler(requestContext);
}
}
public class MyHttpHandler : IHttpHandler
{
RequestContext _requestContext;
public MyHttpHandler(RequestContext requestContext)
{
_requestContext = requestContext;
}
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
var groupName = _requestContext.RouteData.GetRequiredString("group");
// Select the controller, e.g. Home:
_requestContext.RouteData.Values.Add("controller", "Home");
IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory();
IController controller = factory.CreateController(_requestContext, "Home");
if (controller != null)
{
controller.Execute(_requestContext);
}
}
}
这一切都有效,但如果在页面上放置 @Html.ActionLink("Index", "Index") ,则会产生以下链接:
[a href="/?controller=Home"]索引[/a]
有人能告诉我为什么在使用 Html.ActionLink 时要添加控制器参数吗?(我不想将 controller=Home 参数添加到链接中。)
非常感谢!