views:

66

answers:

2

after debbuging 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?

+1  A: 

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

WestDiscGolf
i created controllers for each page. and each controller has action. but I didn't understood how to show just an action in my url. like: localhost:34534/AboutUs etc.
Ragims
If you have one controller and multiple actions its simple (see update). If you have a one to one mapping of controller / action then its doable. However, if you want multiple controllers/actions then it gets tricky as you need to know which controller the action refers to. This could potentially be done with a custom Controller factory and some sort of lookup.
WestDiscGolf
A: 

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" }
);
cottsak
it is exactly opposite of that i want to do :)
Ragims