I need an explanation of MVC routing.

Last post 03-27-2008 9:52 PM by csainty. 5 replies.

Sort Posts:

  • I need an explanation of MVC routing.

    03-26-2008, 2:29 PM

    Hi guys, I have an MVC application going, and as some of you who have used it know that by default you have the "Home" directory, and it has a "HomeController". Well, when the website is live, you always have the "/Home/" in the domain name, like: http://www.mywebsite.com/Home/SomePage. Well, personally that bugs me soooo much. Is there a way to configure the views, the controller and routes to just work like this: http://www.mywebsite.com/SomePage?

    Thoughts appreciated!
     

  • Re: I need an explanation of MVC routing.

    03-26-2008, 3:44 PM
    Answer
    • Loading...
    • srulyt
    • Joined on 02-02-2008, 6:16 PM
    • Posts 208

    you can set a default controller so the controler name does not have to show up in the URI 

    routes.Add(new Route("/{PageName}", new MvcRouteHandler())
                {
                    Defaults = new RouteValueDictionary(new { controller = "Home", action = "LoadPage"})
                });
    you than make an action called "LoadPage" that takes "PageName" as a parameter
    you can aslo make a route called "/{action}" and not set a default action and have an action for every page
  • Re: I need an explanation of MVC routing.

    03-27-2008, 3:20 PM

    Hi srulyt,

    Thanks for the tip, it worked like a charm. If you don't mind, I got another question. So now I have the application working as: http://www.mywebsite.com/SomePage; however, at some point I do need to access a directory, like so: http://www.mywebsite.com/Admin. The Admin directory does need to be handled by a different controller, and it needs its' own routing rules. How do I set it up to work like that. If I try to access it the way it is right now, the application dies and says there was no action for Admin. I get that it's looking at it like a view it needs to render, but it needs to be a directory that works like an independent entity of the rest of the website.

    Thanks again!
     

  • Re: I need an explanation of MVC routing.

    03-27-2008, 4:33 PM

    Hi bulgarian,

    I am not sure if I understood you completely right, but are you looking for something likes this:

     

     routes.Add(new Route("Default.aspx", new MvcRouteHandler())
                {
                    Defaults = new RouteValueDictionary(new { controller = "Admin", action = "RoleManager", id = (string)null }),
                });

     

    And then you can create the Controller AdminController in your Controller-folder. Don't forget to create the folder Admin down in your Views-folder. I hope this is what you were searching for.

    (If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

    Best Regards,
    Johannes

    http://www.irisindividuell.de
    http://www.itecon.de
    http://www.johanneshiemer.de
    Filed under: ,
  • Re: I need an explanation of MVC routing.

    03-27-2008, 4:54 PM

    Well, I just need a directory that I can access that will be controlled by a different controller. After thinking on it, I think I'll go with a sub domain setup, and that will work good enough because I then secure the sub domain and have it work independently like I want it to.

    Thanks for the help again!
     

  • Re: I need an explanation of MVC routing.

    03-27-2008, 9:52 PM
    • Loading...
    • csainty
    • Joined on 03-11-2008, 10:41 PM
    • Newcastle, Australia
    • Posts 48

    You could do it with two routes. One that catches the Admin specal case, and the other as-is above.
    They get processed in order, so be sure to define them in this order.

     routes.Add(new Route("/Admin/{action}", new MvcRouteHandler())
                {
                    Defaults = new RouteValueDictionary(new { controller = "Admin"})
                });

    routes.Add(new Route("/{action}", new MvcRouteHandler())
                {
                    Defaults = new RouteValueDictionary(new { controller = "Home"})
                });

Page 1 of 1 (6 items)