我有一个叫News as Area的模块。在NewsAreaRegistration中,我有
context.MapRoute(
"NewsShow",
"News/{controller}/{friendlyUrlName}/{idNews}",
new { controller = "Show", action = "Index", friendlyUrlName = "", idNews = "" }
);在我的视图中(在主视图文件夹中),我使用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.omething.com/News/ Show /bla-bla-bla/9,没有我在Show controller中拥有的action name Index。我从字面上尝试了这个示例的所有排列,但都不起作用。这有可能吗?
发布于 2012-09-25 22:33:04
好的,所以我试了一下……
路由表:(默认之前)
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我转到的URL:
http://localhost:49840/News/Home/Dude-Check-It-Out/12我还将我的默认路由更改为其他路由,以确保这不是使用默认路由。让我知道这是否有帮助:)
发布于 2012-09-25 22:27:28
您是否将此路由放在默认路由之前?路线位置很重要,从上到下。
发布于 2012-09-25 23:49:57
好吧.我设法工作了。
在我的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 (请注意,我必须编写controller.Not来确定原因:
@Url.RouteUrl(
"NewsShow", new { controller = "Show", friendlyUrlName = FriendlyURL.URLFriendly(true, Model.News.Data.ElementAt(0).Title), idNews = Model.News.Data.ElementAt(0).IdNews }
);https://stackoverflow.com/questions/12585009
复制相似问题