ASP.NET MVC 2.0 中的路由

发布于 2024-09-04 10:21:15 字数 504 浏览 2 评论 0原文

我希望在我的 ASP.NET MVC 2.0 网站中创建一条非常简单的路线。我一直在谷歌上寻求帮助,但我能找到的所有示例都是针对非常复杂的路由的。

基本上我希望主控制器中的所有页面都在域之后解析,而不是 /Home/

例如我想要 http://www.MyWebsite.com/Home/LandingPage/

成为http:// /www.MyWebsite.com/LandingPage/

但仅限于主控制器,我希望其余控制器正常工作。

我考虑过为每个控制器创建一个控制器并仅使用一个索引,但是我们需要大量的登陆页面来进行这样的营销,这会很快使站点加载每个页面的控制器,这不太理想。

I'm looking to make a really simple route in my ASP.NET MVC 2.0 website. I've been googling for help but all the examples I can find are for really complex routing.

Basically I want all the pages in my Home Controller to resolve after the domain as opposed to /Home/

For example I want http://www.MyWebsite.com/Home/LandingPage/

To become http://www.MyWebsite.com/LandingPage/

But only for the Home controller, I want the rest of my controllers to function as normal.

I thought about creating a controller for each and just using an index, but we need lots of landing pages for our marketing like this and it would quickly make the site loaded with controllers for a single page each, which is less than ideal.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你没皮卡萌 2024-09-11 10:21:15

实现此目的的一种方法是为每个着陆页设置单独的路线。另一种方法是使用一条具有与每个着陆页(而不是其他任何内容)匹配的约束的路线。

 routes.MapRoute(
        "LandingPage1"
        "landingpage1/{id}",
        new { controller = "home", action = "landingpage", id = UrlParameter.Optional } );

 routes.MapRoute(
        "LandingPage2"
        "landingpage2/{id}",
        new { controller = "home", action = "landingpage2", id = UrlParameter.Optional } );

请注意,您也可以通过一些反思来做到这一点(未经测试)。

 foreach (var method on typeof(HomeController).GetMethods())
 {
      if (method.ReturnType.IsInstanceOf(typeof(ActionResult)))
      {
          routes.MapRoute(
               method.Name,
               method.Name + "/{id}",
               new { controller = "home", action = method.Name, id = UrlParameter.Optional } );
      }
 }

RouteConstraint 解决方案类似,只是您有一个带有自定义约束的路由,该路由评估适当的路由值是否与 HomeController 上的方法之一匹配,如果匹配,则将控制器和操作替换为“home”,匹配值。

routes.MapRoute(
     "LandingPage",
     "{action}/{id}",
     new { controller = "home", action = "index", id = UrlParameter.Optional },
     new LandingPageRouteConstraint()
);


public LandingPageRouteContstraint : IRouteConstraint
{
    public bool Match
        (
            HttpContextBase httpContext, 
            Route route, 
            string parameterName, 
            RouteValueDictionary values, 
            RouteDirection routeDirection
        )
    {
        // simplistic, you'd also likely need to check that it has the correct return
        // type, ...
        return typeof(HomeController).GetMethod( values.Values["action"] ) != null;
    }
}

请注意,即使使用反射,每页路由机制也仅执行一次。从那时起,您每次都会进行简单的查找。 RouteConstraint 机制每次都会使用反射来查看路由是否匹配(除非它缓存结果,我认为它不会缓存结果)。

One way to do this would be to have a separate route for each landing page. Another way would be to have a single route with a constraint that matches each landing page (and nothing else).

 routes.MapRoute(
        "LandingPage1"
        "landingpage1/{id}",
        new { controller = "home", action = "landingpage", id = UrlParameter.Optional } );

 routes.MapRoute(
        "LandingPage2"
        "landingpage2/{id}",
        new { controller = "home", action = "landingpage2", id = UrlParameter.Optional } );

Note that you could probably do this with a bit of reflection as well (untested).

 foreach (var method on typeof(HomeController).GetMethods())
 {
      if (method.ReturnType.IsInstanceOf(typeof(ActionResult)))
      {
          routes.MapRoute(
               method.Name,
               method.Name + "/{id}",
               new { controller = "home", action = method.Name, id = UrlParameter.Optional } );
      }
 }

The RouteConstraint solution would be similar except that you'd have a single route with a custom constraint that evaluated whether the appropriate route value matched one of the methods on the HomeController and, if so, replaced the controller and action with "home" and the matched value.

routes.MapRoute(
     "LandingPage",
     "{action}/{id}",
     new { controller = "home", action = "index", id = UrlParameter.Optional },
     new LandingPageRouteConstraint()
);


public LandingPageRouteContstraint : IRouteConstraint
{
    public bool Match
        (
            HttpContextBase httpContext, 
            Route route, 
            string parameterName, 
            RouteValueDictionary values, 
            RouteDirection routeDirection
        )
    {
        // simplistic, you'd also likely need to check that it has the correct return
        // type, ...
        return typeof(HomeController).GetMethod( values.Values["action"] ) != null;
    }
}

Note that the route per page mechanism, even if you use reflection, is done only once. From then on you do a simple look up each time. The RouteConstraint mechanism will use reflection each time to see if the route matches (unless it caches the results, which I don't think it does).

执笔绘流年 2024-09-11 10:21:15

我认为您缺少默认路由。

    routes.MapRoute(
   "Default",                     // Route name
   "{controller}/{action}/{id}",  // URL with parameters
   new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
);

因此,当您输入 www.mywebsite.com 时,控制器、操作和 id 参数将具有以下值:

controller : Home    
action: Index    
id : ""

I think you are missing the default route.

    routes.MapRoute(
   "Default",                     // Route name
   "{controller}/{action}/{id}",  // URL with parameters
   new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
);

So, when you type www.mywebsite.com, the controller, action, and id parameters would have the following values:

controller : Home    
action: Index    
id : ""
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文