开发者

asp.net mvc routing - rewrite url

after deb开发者_C百科buging i am getting following url of my webproject:

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:

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?


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. This would create Urls as required with limited configuration of routing.

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

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)

In 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

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://localhost:port/index

http://localhost:port/aboutus


I would have a "Home" controller with the "Home"/"Index" action for "http://localhost:54594/Home". And for "http://localhost:54594/AboutUs" i'd use the "Home" controller too with an action "AboutUs".

For "http://localhost:54594/Products" i'd have it's own controller as it's likely to be more complex. The default route should serve ok for this.

I'd route "/AboutUs" to the "/Home/AboutUs" action with something like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜