开发者

How controller determine which view to use in asp.net mvc

i just strated with asp.net mvc. My doubt is in a controller we use

    public Acti开发者_StackOverflow中文版onResult Index()
    {
        return View();  

    }

so how a relevent view is returned for a particular controller.


Assuming a Home controller with an Index action here are the default search order locations:

ASP.NET MVC 2:

~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx

ASP.NET MVC 3:

~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Home/Index.cshtml
~/Views/Home/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml


Any MVC tutorial will go over this in the first page or two. I would highly recommend you go through either the NerdDinnertutorial or Getting Started with MVC3 or possibly both.

To answer your question, that will return a view called Index in the Views folder that is named after your controller. In other words, if your view is named HomeController, that will return a view named Index in your Home views folder


The default route in ASP.Net MVC is http://.../Controller/Action. So when you are viewing Http://localhost/SomeController/Index, then ASP.Net MVC looks for Index.aspx in designated folders. If it does not find it, then you can see the exception on yellow page that it searched for Index.aspx and Index.ascx in Shared folder and a folder named on your controller.

All this is default behavior and ASP.Net MVC provides you extension points to change some or all of this behavior.


The name of the Function is used to find the name of the View

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜