how does asp.net mvc relate a view to a controller action?

The convention is that if you don’t specify a view name, the corresponding view will be the name of the action. So:

public ActionResult MethodA()
{
    return View();
}

will render ~/Views/ControllerName/MethodA.cshtml.

But you could also specify a view name:

public ActionResult MethodA()
{
    return View("FooBar");
}

and now the ~/Views/ControllerName/FooBar.cshtml view will be rendered.

Or you could even specify a fully qualified view name which is not inside the views folder of the current controller:

public ActionResult MethodA()
{
    return View("~/Views/Foo/Baz.cshtml");
}

Now obviously all this assumes Razor as view engine. If you are using WebForms, replace .cshtml with .aspx or .ascx (if you are working with partials).

For example if there is no view it will even tell you where and in what order is looking for views:

enter image description here

Remember: ASP.NET MVC is all about convention over configuration.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)