自定义URL路由在Asp.Net MVC 4 [英] Custom URL Routing in Asp.Net MVC 4

查看:214
本文介绍了自定义URL路由在Asp.Net MVC 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何才能做到这样的网址( http://www.domain.com/friendly-在Asp.Net MVC内容标题)4。

How can i do like this url (http://www.domain.com/friendly-content-title) in Asp.Net MVC 4.

请注意:此参数始终是动态的。 URL可能是不同的:友好内容标题

Note: This parameter is always dynamic. URL may be different: "friendly-content-title"

我尝试自定义属性,但我没有在的ActionResult赶上这(友好的内容标题)的参数。

I try to Custom Attribute but I dont catch this (friendly-content-title) parameters in ActionResult.

查看:


  • 首页/索引

  • 首页/视频

的ActionResult:

ActionResult:

    // GET: /Home/        
    public ActionResult Index()
    {
        return View(Latest);
    }

    // GET: /Home/Video        
    public ActionResult Video(string permalink)
    {
        var title = permalink;
        return View();
    }

RouteConfig:

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 = "" }
        );

    }

我应该渔获URL(/友好的内容标题)呢?

What should I do for catch to url (/friendly-content-title)?

推荐答案

要启用属性的路由,在配置过程中调用MapMvcAttributeRoutes。以下是code剪断。

To enable attribute routing, call MapMvcAttributeRoutes during configuration. Following are the code snipped.

     public class RouteConfig
        {
            public static void RegisterRoutes(RouteCollection routes)
             {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
                routes.MapMvcAttributeRoutes();
             }
        }

在MVC5,我们可以结合属性与基于约定的路由的路由。以下是code剪断。

In MVC5, we can combine attribute routing with convention-based routing. Following are the code snipped.

        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 }
       );
  }

这是很容易使一个URI参数可选通过添加一个问号路线参数。我们也可以通过使用形式为parameter = value指定一个默认值。 <一href=\"http://www.dotnet-stuff.com/tutorials/aspnet-mvc/understanding-url-rewriting-and-url-attribute-routing-in-asp-net-mvc-mvc5-with-examples\"相对=nofollow>这里是完整的文章。

这篇关于自定义URL路由在Asp.Net MVC 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆