【发布时间】:2017-09-14 09:23:04
【问题描述】:
我试图让我的网址看起来不错,并且对蛞蝓的 seo 友好。我坚持我成功了,但后来我的默认路由停止工作。 当我转到这个 example.com/location/viewlocation/528 时,网址最终会像 example.com/528/a-nice-location
那就太好了! 但是现在我的正常东西不起作用。 输入 example.com/home/index 会导致错误
参数字典包含“Oplev.Controllers”中方法“System.Web.Mvc.ActionResult ViewLocation(Int32, System.String)”的不可为空类型“System.Int32”的参数“id”的空条目。位置控制器'。可选参数必须是引用类型、可空类型或声明为可选参数。
我尝试了不同的解决方案,但我遗漏了一些东西。不能让它工作。
我的代码: 路由配置
routes.MapRoute(
name: "view_location",
url: "{id}/{slug}",
defaults: new { controller = "Location", action = "ViewLocation", id = UrlParameter.Optional, slug = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
位置控制器
public ActionResult ViewLocation(int id, string slug)
{
if (string.IsNullOrEmpty(slug))
{
slug = "a-nice-location"; // testing..
return RedirectToRoute("view_location", new { id = id, slug = slug });
}
return View();
}
家庭控制器
public ActionResult Index()
{
return View();
}
【问题讨论】:
标签: asp.net-mvc url-routing slug