Showing posts with label MapRoute. Show all posts
Showing posts with label MapRoute. Show all posts

Saturday, 9 January 2010

MVC ASP.Net URL 2(Get the Value from URL and show it on Page)

In my last post we worked on getting string from URL and show it to the page with Welcome. There was one issue with URL which we are going to discuss with this post so we are going to start with the same project if you didn't follow the previous post here is the link and can download the previous project from here.

Now the issue was if we type the string with this URL
http://localhost:61545/Kamran with out /Home/Index we get this error window.





We already saw the default route in our previous post and that's what the only route we have if we type different URL our website cannot recognise it because there is no route defined at Global.asax file. Now we are going to define new two routes for this example which are here at Global.asax file.

routes.MapRoute(

"First", // Route name

"{name}", // URL with parameters

new { controller = "Home", action = "Index", name = "" } // Parameter defaults

);

routes.MapRoute(

"Second", // Route name

"{controller}/{name}", // URL with parameters

new { controller = "Home", action = "Index", name = "" } // Parameter defaults

);




These both routes should be defined before Default route which was:

routes.MapRoute(

"Default", // Route name

"{controller}/{action}/{name}", // URL with parameters

new { controller = "Home", action = "Index", name = "" } // Parameter defaults

);



If they come after Default route than application will take in account the Default one.
Now the Global.asax file will look like:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using System.Web.Routing;

namespace URLExample

{

// Note: For instructions on enabling IIS6 or IIS7 classic mode,

// visit http://go.microsoft.com/?LinkId=9394801

public class MvcApplication : System.Web.HttpApplication

{

public static void RegisterRoutes(RouteCollection routes)

{

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(

"First", // Route name

"{name}", // URL with parameters

new { controller = "Home", action = "Index", name = "" } // Parameter defaults

);

routes.MapRoute(

"Second", // Route name

"{controller}/{name}", // URL with parameters

new { controller = "Home", action = "Index", name = "" } // Parameter defaults

);

routes.MapRoute(

"Default", // Route name

"{controller}/{action}/{name}", // URL with parameters

new { controller = "Home", action = "Index", name = "" } // Parameter defaults

);

}

protected void Application_Start()

{

RegisterRoutes(RouteTable.Routes);

}

}

}



Finally run the application and type: http://localhost:61545/Kamran the result will be:




Now try another Route which is defined in "Second" : http://localhost:61545/Home/Kamran the result will be same as we already defined the Route for this URL.

Thanks to all of you who follow my post and thanks to all your support please don't forget to comment and will come back with URL Constraints.