0

I created a sample MVC application using following Template.

ASP.NET MVC2 Empty Web Application

Then a added a Controller with the name of First and a right clicked the ActionResult to add a View.

I typed http://localhost:49565/First in my Browser.

Query

How is the controller internally getting to know that a specific page will be displayed when we will type http://localhost:49565/First ?

Moreover, If I add multiple Views for a Controller. How will the system decide which one will be displayed on Priority ?

2
  • 1
    Welcome to stackoverflow. Your question about MVC is on the very, very basic elements of the framework. I would suggest you look at one of the many tutorials for beginners on the web before you post questions here. e.g: asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/… Commented May 6, 2012 at 10:26
  • Welcome to my first question in SO. This is a very specific question after reading MVC tutorials. Do you know the answer? Commented May 6, 2012 at 10:31

2 Answers 2

1

The controller is invoked by the MVC framework, which uses the routes defined in Global.asax.cs to determine which controller and action to invoke. There is a default route that looks like this:

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

When the application receives a request is will try to parse the URL to the format of the routes. If the request is made to http://localhost:49565/, it will use the default values which goes to the Index action in the controller named HomeController. When you have created the new controller, FirstController, and call http://localhost:49565/First, it uses the FirstController instead of the HomeController since it has been provided (but still to the Index action).

Further, when an action is being invoked and there is no view defined explicitly, it will look for a view named the same as the invoked action. In your case it would be ~/Views/First/Index.aspx.

EDIT

If you want to use another view you can specify it in the return statement

return View("OtherView");

and it will use ~/Views/First/OtherView.aspx instead.

Sign up to request clarification or add additional context in comments.

2 Comments

I have two Views 1. Index.aspx and View1.aspx Whenever I type http://localhost:49565/First in my Browser, How can I invoke View1.aspx. Right now it is always showing Index.aspx page.
Thanks it helped a lot. I could not upvote due to less reputations.:)
0

Have a look at this blog posts give u the idea of how it is done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.