HappyHands31
HappyHands31

Reputation: 4101

How To Change Default View In MVC 4 Web Application

By default, MVC 4 in Visual Studio 2017 sets _Layout.cshtml as the default layout for all pages. I believe it's doing this in App_Start/RouteConfig.cs:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

(Index is set as the home page)

enter image description here

I'm still not sure how Index is getting _Layout.cshtml. But what if I'm trying to set a different view - a login page - as the home page, like this?

enter image description here

Also I'm trying to get rid of the Reports, Accounts, Settings, and Logout <li>'s in the header so that the page matches the design above. I'll also need a container with a form inside of it.

I've tried creating a _Login view inside of /Home and /Shared and changed "Index" to "Login" in App_Start/RouteConfig.cs:

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Login", id = UrlParameter.Optional }

enter image description here

But that's giving me an error:

enter image description here

How can I create a view and set that view as the default view for this MVC 4 Web App? Thanks

Upvotes: 3

Views: 14449

Answers (2)

Hossein Narimani Rad
Hossein Narimani Rad

Reputation: 32481

What you see in the defaults parameters as action is the name of the controller's method, not view, so you should create a method named Login in the Home controller and create the associated View for that (In the Login method right click and choose Add View). Then it will act as the default home page.

defaults: new { controller = "Home", action = "Login", id = UrlParameter.Optional }

So your Home controller looks like this:

public class HomeController : Controller
{
    public IActionResult Login()
    {
        return View();
    }
    //other codes   
 }

Also if you do not want the default layout to be used in the Login page you may add this at top of the Login page

@{
    Layout = "";
}

Upvotes: 4

Prashanth Benny
Prashanth Benny

Reputation: 1608

The error you see doesn't seem to be because of the layout page.

This error is because the Login Action is missing in the Home controller.

You see, The defaults specified are Controlller="Home", Action="Login".
i.e. The compiler looks for the Login action in the Home controller. and when it doesn't find, it throws this error!

you could get rid of it by adding the login action like:

public ActionResult Login(string Uname, string Password)
{
    return View();
}

in the home controller! That's for the error in the question.

Here is the solution for your problem. You could add a different layout for each of your views by adding a razor code like below, to specify the layout for the view.

@{
   Layout = "~/Views/Shared/_Layout.cshtml";
                  //This is the path to the layout. You could change this to your custom layout's path.
}

Hope this helps!

Upvotes: 2

Related Questions