mst*_*cck 2 c# asp.net-mvc url-routing custom-url asp.net-mvc-4
我如何在Asp.Net MVC 4中使用此URL(http://www.domain.com/friendly-content-title).
注意:此参数始终是动态的.网址可能有所不同:"friendly-content-title"
我尝试自定义属性,但我没有在ActionResult中捕获这个(友好内容标题)参数.
浏览次数:
的ActionResult:
// GET: /Home/
public ActionResult Index()
{
return View(Latest);
}
// GET: /Home/Video
public ActionResult Video(string permalink)
{
var title = permalink;
return View();
}
Run Code Online (Sandbox Code Playgroud)
RouteConfig:
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 = "" }
);
}
Run Code Online (Sandbox Code Playgroud)
我应该怎样做才能捕获到url(/ friendly-content-title)?
小智 6
要启用属性路由,请在配置期间调用MapMvcAttributeRoutes.以下是代码剪切.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
}
}
Run Code Online (Sandbox Code Playgroud)
在MVC5中,我们可以将属性路由与基于约定的路由相结合.以下是代码剪切.
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 }
);
}
Run Code Online (Sandbox Code Playgroud)
通过向路由参数添加问号,可以非常轻松地将URI参数设置为可选.我们还可以使用form parameter = value指定默认值.这是完整的文章.
| 归档时间: |
|
| 查看次数: |
14292 次 |
| 最近记录: |