C# Asp.Net MVC-仅参数操作请求
我有一个Asp.NETMVC项目,我们允许我们的用户拥有公共配置文件 我想改进url,使其更友好、更短 现行守则如下:C# Asp.Net MVC-仅参数操作请求,c#,.net,asp.net-mvc,url-routing,asp.net-mvc-routing,C#,.net,Asp.net Mvc,Url Routing,Asp.net Mvc Routing,我有一个Asp.NETMVC项目,我们允许我们的用户拥有公共配置文件 我想改进url,使其更友好、更短 现行守则如下: public class ProfileController : Controller { private readonly IUserProfileService _userProfileService; public ProfileController(IUserProfileService userProfileService)
public class ProfileController : Controller
{
private readonly IUserProfileService _userProfileService;
public ProfileController(IUserProfileService userProfileService)
{
this._userProfileService = userProfileService;
}
public ActionResult Index(string id)
{
//Get users profile from the database using the id
var viewModel = _userProfileService.Get(id);
return View(viewModel);
}
}
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//Required for the route prefix attributes to work!
routes.MapMvcAttributeRoutes();
routes.MapRoute(
"ProfileUrlIndexActionRemoval",
"Profile/{id}",
new { controller = "Profile", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
上述代码允许以下url基于默认MVC路由工作-www.mydomain.com/profile/john-doe
我需要实现什么样的路由,才能让下面的url工作——www.mydomain.com/john-doe
谢谢。我会这样做的。注册与根斜杠后的任何字符串匹配的路由 请注意,这严重限制了应用程序可以使用的路由,因为并非所有匹配/{id}的内容实际上都是用户id,这就是为什么应用程序通常会在路由前面加上/profile或/p
这有点棘手,因为您希望友好的URL位于站点的根目录中,而不与任何其他路由冲突 这意味着,如果您有任何其他路由,如About或Contact,则需要确保在友好路由之前,这些路由在路由表中,以避免路由冲突
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//Required for the route prefix attributes to work!
routes.MapMvcAttributeRoutes();
routes.MapRoute(
"ProfileUrlIndexActionRemoval",
"Profile/{id}",
new { controller = "Profile", action = "Index" }
);
routes.MapRoute(
name: "Home",
url: "Home/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "About",
url: "About/{action}/{id}",
defaults: new { controller = "About", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Contact",
url: "Contact/{action}/{id}",
defaults: new { controller = "Contact", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Default_Frieldly",
"{*id}",
new { controller = "Profile", action = "Index" }
);
}
}
最后,由于默认路由将捕获所有不匹配的路由,因此需要考虑未找到的配置文件
public class ProfileController : Controller {
//...code removed for brevity
public ActionResult Index(string id) {
//Get users profile from the database using the id
var viewModel = _userProfileService.Get(id);
if(viewModel == null) {
return NotFound();
}
return View(viewModel);
}
}
通过在原始URL中使用profile controller前缀,使其具有唯一性,以避免路由冲突,但是在想要根友好的URL时,虽然并非不可能,但您可以看到为了获得所需的行为而需要跳过的环
public class ProfileController : Controller {
//...code removed for brevity
public ActionResult Index(string id) {
//Get users profile from the database using the id
var viewModel = _userProfileService.Get(id);
if(viewModel == null) {
return NotFound();
}
return View(viewModel);
}
}