繁体   English   中英

Asp.Net MVC 4中的自定义URL路由

[英]Custom URL Routing in 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();
    }

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(/ friendly-content-title)?

要启用属性路由,请在配置期间调用MapMvcAttributeRoutes。 以下是代码剪切。

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

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

通过向路由参数添加问号,可以非常轻松地将URI参数设置为可选。 我们还可以使用form parameter = value指定默认值。 是完整的文章。

RadimKö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()});

那么在你的家庭控制器上,你可以采取这样的行动

HomeController.cs

public ActionResult PermaLink(string customRoute)
{
    //customRoute would be /friendly-content-title..do what you want with it
}

这种情况发生在我们指定为MapRoute调用中第4个参数的IRouteConstraint中。

PermaLinkRouteConstraint.cs

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方法中进行数据库调用或任何缓慢的操作,因为我们设置为每个发送到您网站的请求调用(您可以移动它以便以不同的顺序调用) )。

如果适合你,我会选择RadimKöhler的解决方案。

我们需要的是一些标记关键字。 要清楚地说,网址应该被视为动态网址,具有friendly-content-title 我建议使用关键字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 (如下所示)将被视为动态:

// these will be processed by Video action of the Home controller
domain/video/friendly-content-title 
domain/video/friendly-content-title2 

而任何其他( controllerName/ActionName )将以标准方式处理

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM