Thursday 31 December 2009

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

This is really a basic example. Our task is to get the value from URL and print it on Index page like:

http://localhost:59451/Home/Index/Kamran

So what we going to do is to take that last word which is string “Kamran” and print it with Welcome like:





Open Visual Studio and select the ASP.Net MVC Web Application project and change the name to “URLExample”. Skip the step to create the Test Case for now.




The project is created and Solution Explorer has different folders and files added already for us:



Now first of all we need to look to the file where these URL route defined. Open the file Global.asax.

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(

"Default", // Route name

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

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

);

}

protected void Application_Start()

{

RegisterRoutes(RouteTable.Routes);

}

}

}


At this point there is already one MapRoute defined which has Controller/Action/Id as a URL. Here Controller is set by default to Home, Action is set to Index and Id to “” which tells us if the URL we type is http://localhost:59451/ and this MapRoute default setting will convert it to http://localhost:59451/Home/Index/ and we will see the simple home page. Now we are going to make some changes here first of all change Id to Name:

From:

routes.MapRoute(

"Default", // Route name

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

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

);

To:

routes.MapRoute(

"Default", // Route name

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

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

);

Now open the file HomeController.cs in Controllers folder the Action is define as Index which called when we hit the Index in URL but this action currently don’t accept any parameter:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

namespace URLExample.Controllers

{

[HandleError]

public class HomeController : Controller

{

public ActionResult Index()

{

ViewData["Message"] = "Welcome to ASP.NET MVC!";

return View();

}

public ActionResult About()

{

return View();

}

}

}

Change this action to accept string and also concatinate the vaoue of string to ViewDate[“Message”]:

From:

public ActionResult Index()

{

ViewData["Message"] = "Welcome to ASP.NET MVC!";

return View();

}

To:

public ActionResult Index(string name)

{

ViewData["Message"] = "Welcome " + name;

return View();

}

Now if you run your solution and type the URL you will following result:





Here one issue if you remove the /Home/Index from the URL and only type this URL http://localhost:61545/Kamran you will get this error.

We will see how to resolve this error in my next post. Stay with me.



Download code here.