简体   繁体   English

ASP.NET MVC路由-重写URL

[英]asp.net mvc routing - rewrite url

after debbuging i am getting following url of my webproject: 调试后,我得到了我的webproject的URL:

http://localhost:54594/Home/Home                  /Home-Controller/Home-Action
http://localhost:54594/AboutUs/AboutUs            /AboutUs-Controller/AboutUs-Action
http://localhost:54594/Products/Products          /Products-Controller/Products-Action

In my global.asax i have: 在我的global.asax我有:

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

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

I want to show my URLs like this: 我想显示我的网址,如下所示:

http://localhost:54594/Home          /Home action or controller
http://localhost:54594/AboutUs       /AboutUs action or controller
http://localhost:54594/Products      /Products action or controller

With Home.aspx this works alright because it's my default url, how can I do same with the rest of urls? 使用Home.aspx可以正常工作,因为这是我的默认网址,我该如何处理其余网址?

You can do it a couple of ways, one way would be to have a controller for each area and each controller to have one Index action. 您可以通过两种方法来实现,一种方法是为每个区域都具有一个控制器,并且每个控制器都具有一个Index动作。 This would create Urls as required with limited configuration of routing. 这将在有限的路由配置下根据需要创建Urls。

The other method would be to have one controller and multiple actions (one for each Home , AboutUs, Products) and set the route to be something like this (untested) ... 另一种方法是有一个控制器和多个动作(每个Home,AboutUs,Products都有一个动作),并将路由设置为这样(未测试)...

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

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

Hope this helps. 希望这可以帮助。

Edit 编辑

Just checked the following code with the following assumptions - 只需按照以下假设检查以下代码-

1 Controller (HomeController) 1个控制器(HomeController)

In Global.ascx 在Global.ascx中

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

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

    }

In HomeController 在HomeController中

public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewData["Message"] = "Welcome to ASP.NET MVC!";

        return View();
    }

    public ActionResult AboutUs()
    {
        return View();
    }
}

This will allow for: 这将允许:

http://localhost:port/ HTTP://本地主机:端口/

http://localhost:port/index HTTP://本地主机:端口/索引

http://localhost:port/aboutus HTTP://本地主机:端口/关于我们

I would have a "Home" controller with the "Home"/"Index" action for "http://localhost:54594/Home". 我将使用“ http:// localhost:54594 / Home”的“ Home” /“ Index”操作的“ Home”控制器。 And for "http://localhost:54594/AboutUs" i'd use the "Home" controller too with an action "AboutUs". 对于“ http:// localhost:54594 / AboutUs”,我也将使用“ Home”控制器以及一个“ AboutUs”操作。

For "http://localhost:54594/Products" i'd have it's own controller as it's likely to be more complex. 对于“ http:// localhost:54594 / Products”,我想拥有它自己的控制器,因为它可能更复杂。 The default route should serve ok for this. 默认路由应该可以。

I'd route "/AboutUs" to the "/Home/AboutUs" action with something like this: 我会将“ / AboutUs”路由到“ / Home / AboutUs”操作,如下所示:

routes.MapRoute(
    "AboutUs_Shortcut", "AboutUs", new { controller = "Home", action = "AboutUs" }
);

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

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