C# Asp.NETMVC4中的自定义URL路由
如何在Asp.NETMVC4中使用此url() 注意:此参数始终是动态的URL可能不同:“友好内容标题” 我尝试自定义属性,但在ActionResult中找不到这个(友好内容标题)参数 观点:C# Asp.NETMVC4中的自定义URL路由,c#,asp.net-mvc,asp.net-mvc-4,url-routing,custom-url,C#,Asp.net Mvc,Asp.net Mvc 4,Url Routing,Custom Url,如何在Asp.NETMVC4中使用此url() 注意:此参数始终是动态的URL可能不同:“友好内容标题” 我尝试自定义属性,但在ActionResult中找不到这个(友好内容标题)参数 观点: 主页/索引 家庭/视频 行动结果: // GET: /Home/ public ActionResult Index() { return View(Latest); } // GET: /Home/Video
- 主页/索引
- 家庭/视频
// GET: /Home/
public ActionResult Index()
{
return View(Latest);
}
// GET: /Home/Video
public ActionResult Video(string permalink)
{
var title = permalink;
return View();
}
路线图:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Home Page",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Video Page",
url: "{Home}/{permalink}",
defaults: new { controller = "Home", action = "Video", permalink = "" }
);
}
我应该怎么做捕获到url(/友好内容标题)?我们需要的是一些标记关键字。明确地说,url应被视为动态url,并具有友好的内容标题。我建议使用关键字
video
,然后这将是路线的映射:
routes.MapRoute(
name: "VideoPage",
url: "video/{permalink}",
defaults: new { controller = "Home", action = "Video", permalink = "" }
);
routes.MapRoute(
name: "HomePage",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index" }
);
现在,由于VideoPage
被声明为第一个URL,因此所有URL(如下所示)都将被视为动态URL:
// these will be processed by Video action of the Home controller
domain/video/friendly-content-title
domain/video/friendly-content-title2
而任何其他(
controllerName/ActionName
)都将以标准方式处理Radim Köhler的解决方案是一个很好的解决方案
但是,如果您想要对路由进行更多控制,另一个选项是使用自定义约束
这里有一个例子
RouteConfig.cs
routes.MapRoute(
"PermaLinkRoute", //name of route
"{*customRoute}", //url - this pretty much catches everything
new {controller = "Home", action = "PermaLink", customRoute = UrlParameter.Optional},
new {customRoute = new PermaLinkRouteConstraint()});
public ActionResult PermaLink(string customRoute)
{
//customRoute would be /friendly-content-title..do what you want with it
}
public class PermaLinkRouteConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values,
RouteDirection routeDirection)
{
var permaRoute = values[parameterName] as string;
if (permaRoute == null)
return false;
if (permaRoute == "friendly-content-title")
return true; //this indicates we should handle this route with our action specified
return false; //false means nope, this isn't a route we should handle
}
}
那么在你的家庭控制器上,你可以有这样的动作
HomeController.cs
routes.MapRoute(
"PermaLinkRoute", //name of route
"{*customRoute}", //url - this pretty much catches everything
new {controller = "Home", action = "PermaLink", customRoute = UrlParameter.Optional},
new {customRoute = new PermaLinkRouteConstraint()});
public ActionResult PermaLink(string customRoute)
{
//customRoute would be /friendly-content-title..do what you want with it
}
public class PermaLinkRouteConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values,
RouteDirection routeDirection)
{
var permaRoute = values[parameterName] as string;
if (permaRoute == null)
return false;
if (permaRoute == "friendly-content-title")
return true; //this indicates we should handle this route with our action specified
return false; //false means nope, this isn't a route we should handle
}
}
这一点的神奇之处在于我们在MapRoute
调用中指定为第四个参数的iroutConstraint
PermaLinkRouteConstraint.cs
routes.MapRoute(
"PermaLinkRoute", //name of route
"{*customRoute}", //url - this pretty much catches everything
new {controller = "Home", action = "PermaLink", customRoute = UrlParameter.Optional},
new {customRoute = new PermaLinkRouteConstraint()});
public ActionResult PermaLink(string customRoute)
{
//customRoute would be /friendly-content-title..do what you want with it
}
public class PermaLinkRouteConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values,
RouteDirection routeDirection)
{
var permaRoute = values[parameterName] as string;
if (permaRoute == null)
return false;
if (permaRoute == "friendly-content-title")
return true; //this indicates we should handle this route with our action specified
return false; //false means nope, this isn't a route we should handle
}
}
我只是想展示一个像这样的解决方案,来展示你基本上可以做任何你想做的事情
显然,这需要调整。另外,在Match
方法中,您还必须小心不要有数据库调用或任何缓慢的操作,因为我们将其设置为对每个发送到您的网站的请求进行调用(您可以将其移动,以不同的顺序进行调用)
如果Radim Köhler的解决方案适合您,我会选择它。要启用属性路由,请在配置过程中调用MapMVCattributerRoutes。以下是截取的代码
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
}
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
在MVC5中,我们可以将属性路由与基于约定的路由相结合。以下是截取的代码
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
}
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
通过在route参数中添加问号,可以很容易地使URI参数成为可选参数。我们还可以使用form参数=value指定默认值。这是整篇文章。这里的限制可能会有点困难,即如果友好的标题可能会有很大的不同。关键词(例如,
视频
)很可能会简化内容。但我真的很喜欢你说的。因为真的,有了iroutConstraint
,我们可以创造很多奇迹。也许,即使在这种情况下,它也可能是合适的。我投你一票;)@mstfcck我不理解你的评论。这将让你处理你想要的任何友好标题。如果两个发布的解决方案都不适合你,你能更具体地说明你在寻找什么吗?我试试看。但我不想那样。我想我确实明白。。。您不喜欢视频/
部分。但我想说,这将是最合适的方式——我想说。为什么?因为如果没有这个关键字,路由机制将永远无法确定url domain/home是控制器还是视频(home)的名称。最后,我想说的是,关键字(一些合理的;)甚至可以帮助您的用户理解域/video/…背后的内容。。。