【发布时间】:2013-06-07 08:45:24
【问题描述】:
我正在使用 asp.net mvc 4 。我想使用 3-4 路线模式,但我不能。它只适用于一种模式: 这是我的 RouteConfig 文件:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(
new Route("mycontroller/{action}/{mlid}/{countryid}/{cityid}",
new RouteValueDictionary(
new
{ action="myaction",
mlid = UrlParameter.Optional,
countryid = UrlParameter.Optional,
cityid = UrlParameter.Optional,
}),
new HyphenatedRouteHandler())
);
routes.Add(
new Route("{controller}/{action}/{id}",
new RouteValueDictionary(
new { controller = "start", action = "Index",id = UrlParameter.Optional}),
new HyphenatedRouteHandler())
);
我有一个带有 8 个参数的控制器。我想使用第一种模式,但它使用第二种模式。我使用了 Route Debugger,但它对我没有帮助。 请帮忙。
编辑:
我使用此代码进行导航:
<a href="@Url.Action("myaction", "mycontroller", new { mlid = "1" })">test</a>
但它会在地址栏中显示:
http://localhost:12911/mycontroller/myaction?mlid=1
我想展示一下:
http://localhost:12911/mycontroller/myaction/1
编辑:
这是我的课:
public class HyphenatedRouteHandler : MvcRouteHandler
{
protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
{
if (requestContext.RouteData.DataTokens["area"] != null)
{
requestContext.RouteData.DataTokens["area"] = requestContext.RouteData.Values["area"].ToString().Replace('-', '_');
}
requestContext.RouteData.Values["controller"] = requestContext.RouteData.Values["controller"].ToString().Replace("-", "_");
requestContext.RouteData.Values["action"] = requestContext.RouteData.Values["action"].ToString().Replace("-", "_");
return base.GetHttpHandler(requestContext);
}
}
【问题讨论】:
-
您希望您的路线是什么样的?
-
@edhedges :我编辑它。
标签: asp.net-mvc asp.net-mvc-4 asp.net-mvc-routing