路由在ASP.NET MVC 2.0 [英] Routing in ASP.NET MVC 2.0

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

问题描述

我在寻找使我的ASP.NET MVC 2.0网站的一个非常简单的路线。我一直在使用Google的帮助,但所有我能找到的例子是非常复杂的路由。

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.

基本上我想在我的家庭控制器的所有页面,而不是到/ home /

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

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

要成为 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.

推荐答案

一做这将是为每个着陆页的独立路线的方式。另一种方法是与每一个登陆页面(没有别的)匹配约束的一条路由。

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

该RouteConstraint的解决办法是,只是你必须与评估合适的路由值是否匹配对HomeController的方法之一,如果是这样,更换控制器和行动家里定制约束的单一路线相似和匹配的值。

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

注意,每页机制的途径,即使你使用反射,只需进行一次。从此,你做一个简单的查找各一次。该RouteConstraint机制将使用反射每次看是否路由匹配(除非它缓存的结果,我不认为它)。

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).

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

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