Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# MVC routing - multiple routes

I have a default c# mvc routes:

routes.MapRoute(

    "Default",

    "{controller}/{action}/{id}"

    new { controller = "Home", action = "Index", id = "Welcome" }

);

Now I will get urls like:

mysite.com/Home/Index/Page1
mysite.com/Home/Index/Page2
mysite.com/Home/Index/Page3
mysite.com/Account/Login
mysite.com/Account/Etc

But I would like to have the first set with a shorter url like:

mysite.com/Page1
mysite.com/Page2
mysite.com/Page3
mysite.com/Account/Login
mysite.com/Account/Etc

I expected the code to be really simple like:

routes.MapRoute(

    "Shorturl",

    "{id}",

    new { controller = "Home", action = "Index", id = "Welcome" } 

);

routes.MapRoute(

    "Default",

    "{controller}/{action}/{id}"

    new { controller = "Home", action = "Index", id = "Welcome" }

);

But that doesn't work. It will only take the first route and forget the second. How can you make your program take the first route when there is only one parameter (like mysite.com/Page1) and take the second route when you have multiple routes (like mysite.com/Account/Login) ?

Edit: I can do:

routes.MapRoute("Short", "short/{id}", new { controller = "Home", action = "Indx", id = "Page1" } );

But then I would have an ugly "short/" in the url. I can fix it with:

routes.MapRoute("Page1", "Page1", new { controller = "Home", action = "Index", id = "Page1" } );

But then I need to add each new page manually...

like image 475
Paul Avatar asked Dec 23 '11 09:12

Paul


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C or C++ same?

While C and C++ may sound similar, their features and usage differ. C is a procedural programming language that support objects and classes. On the other hand C++ is an enhanced version of C programming with object-oriented programming support.

What is the full name of C in C?

Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'. So ++ being the increment operator, C has an incremented version called as “C++”.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

You might want to try something like this.

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

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

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

    }

make sure that you add this to the route before the default(or even remove the default if you want)

But the order in which these are added is important.

There was one bit of info missing, and that the Action within the controller.

public ActionResult Index(string id)
{
      ViewBag.Message = "Welcome to ASP.NET MVC!"+id;
      return View();
}

Hope this helps.

Regards.

like image 77
Sitnam Avatar answered Oct 15 '22 23:10

Sitnam