我有一个名为News as Area的模块。在NewsAreaRegistration我有
context.MapRoute(
"NewsShow",
"News/{controller}/{friendlyUrlName}/{idNews}",
new { controller = "Show", action = "Index", friendlyUrlName = "", idNews = "" }
);
在我的视图中(在主View文件夹中)我使用RouteUrl方法来强制执行我的自定义路径
@Url.RouteUrl("NewsShow", new { controller = "Show", action = "Index", friendlyUrlName = FriendlyURL.URLFriendly(true, Model.News.Data.ElementAt(0).Title), idNews = Model.News.Data.ElementAt(0).IdNews})"
我想做的是这样的路线www.something.com/News/Show/bla-bla-bla/9 没有动作名称我在Show controller中有索引。我尝试了这个例子的所有排列,但没有任何效果。这甚至可能吗?
答案 0 :(得分:2)
好的,我试过了......
路由表:(默认情况下)
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
routes.MapRoute(
name: "Hidden",
url: "News/{controller}/{friendlyUrlName}/{idNews}",
defaults: new {controller = "Home", action = "Index", friendlyUrlName = "", idNews = ""});
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Index", id = UrlParameter.Optional }
);
在视图中:
@Url.RouteUrl("Hidden", new { friendlyUrlName = "Dude-Check-It-Out", idNews = 12 })
在我的控制器中:
public ActionResult Index(string friendlyUrlName, int idNews)
{
ViewBag.Message = "Modify this template to kick-start your ASP.NET MVC application.";
ViewBag.UrlName = friendlyUrlName;
ViewBag.NewsId = idNews;
return View();
}
我得到了这个......
/News/Home/Dude-Check-It-Out/12
我转到的网址:
http://localhost:49840/News/Home/Dude-Check-It-Out/12
我还将默认路由更改为其他内容以确保不使用默认路由。如果这有帮助,请告诉我:)。
答案 1 :(得分:1)
您是否将此路线置于默认路线之前?路线位置从上到下很重要。
答案 2 :(得分:0)
好的......我设法以某种方式工作。
在我的NewsAreaRegistration中,我必须在默认情况下移动NewsShow路由。不知道为什么,因为RouteUrl应该兴奋地映射到NewsShow。
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"NewsShow",
"News/{controller}/{friendlyUrlName}/{idNews}",
new { controller = "Show", action = "Index", friendlyUrlName = "", idNews = "" }
);
context.MapRoute(
"News_default",
"News/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
这是我的RouteUrl(注意我必须编写控制器。不确定为什么:
@Url.RouteUrl(
"NewsShow", new { controller = "Show", friendlyUrlName = FriendlyURL.URLFriendly(true, Model.News.Data.ElementAt(0).Title), idNews = Model.News.Data.ElementAt(0).IdNews }
);