MVC中的多主体页面

时间:2016-04-28 12:41:00

标签: asp.net asp.net-mvc

我有以下几页

http://example.com/color/red 
http://example.com/car/escort

在ASP.NET Webform中,我可以使用URL Rewrite重写两个页面,以便规则将页面重写为

http://example.com/process.aspx?subject=color&type={1}
http://example.com/process.aspx?subject=car&type={1}

然后Process.aspx完成所有工作。

有没有一种方法可以在不使用IIS Rewrite的情况下在MVC中模拟上述内容?这是在MVC路由或其他地方处理的,我需要IIS Rewrite来帮助我吗?

1 个答案:

答案 0 :(得分:0)

您可以使用MVC路由。假设http://example.com/将调用Home控制器上的索引方法。您可以在默认MapRoute

之前添加
routes.MapRoute(
                    name: "RouteWithColor",
                    url: "Color/{Color}",
                    defaults: new { controller = "Home", action = "Index" }
                );

   routes.MapRoute(
    name: "RouteWithCar",
    url: "car/{car}",
    defaults: new { controller = "Home", action = "Index" }
         );

然后在你的控制器中

        var car = RouteData.Values["Car"];
        var color = RouteData.Values["Color"];
     

如果用户使用http://example.com/Car/somecare导航到该页面,您将获得值作为变量。因此,您可以检查您的变量是否为空并继续处理