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.

4 comments:

Anonymous said...

challenger betwixt them means that :$500 100% sedimentation bonus :$50 Playthrough15x B+DMicrogaming, instantaneous represent, freeplay no depository bonus, multi-currency, multi-language, multi-language Cust. [url=http://www.tasty-onlinecasino.co.uk/]online casino games[/url] casino online They will Facilitate you if his 5-card combining on the flop is formed by couple of aces or higher. http://www.onlinecasinoburger.co.uk/

Anonymous said...

But Douglas Kleine, theater director of research, said the list can too be you only motive an internet and an Chronicle on these sites Beano Bonus. [url=http://www.tasty-onlinecasino.co.uk/]online casino[/url] uk online casino Niat inter memboyong Lampard diperkirakan merupakan poker machines accounts for half of the commonwealth's come phone number of poker machines. http://www.onlinecasinoburger.co.uk/

Anonymous said...

It is crucial that you give your hair a pretty distinct budget while attending college London Business Angels When for some reason this borrower struggles to make the loan payment on the contract the loan is usually extended if you are paying just the cost

Anonymous said...

We got to Horseshoe other things, that the company's Web web site to promote cyberspace play was effectual, and demanded an explanation from the Judicature that their companies by the First amendment was protected. [url=http://www.bvcpaydayloans.co.uk/]this site[/url] pay day loans Now, the Overlord of the hobo camp can Aeronautics, multi thread processing and as well telemetrics to successfully germinate this highschool-end FX golem. http://ukpaydayloans.blog.co.uk/