【发布时间】:2014-01-11 04:20:09
【问题描述】:
这是我第一次在 ASP.Net Mvc 上处理路由,我正在尝试像 StackOverflow 那样处理它的问题。
我的控制器名为News,它有他的动作,如News/、News/Create、News/Edit/1 等。我想添加这个自定义路由News/1,它将返回新闻本身的可视化,而不是默认索引News/ 显示的网格。
这些是我的路线:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "ViewNews",
url: "{controller}/News/{newsId}",
defaults: new { controller = "News", action = "Index" },
constraints: new { newsId = @"\d+" }
);
第一个是默认路由,第二个是我现在尝试的,跟随this post。但它只是给了我这个错误(在这个网址上News/1):
“/”应用程序中的服务器错误。
找不到资源。
我想知道我做错了什么,一旦它甚至没有达到行动。如果我尝试News/,效果很好。
我已经通过我的行动做到了这一点:
public ActionResult Index(int? id)
{
if (id != null)
{
var news = _newsService.GetView((int)id);
if (news != null)
{
return View("News", news);
}
else
{
return RedirectToAction("Index", "Home");
}
}
return View();
}
如果有人能告诉我该怎么做,那就太好了:News/1/news-title-here。
【问题讨论】:
标签: c# asp.net asp.net-mvc asp.net-mvc-4 asp.net-mvc-routing