How To Change Default View In MVC 4 Web Application

11,152

Solution 1

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 = "";
}

Solution 2

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!

Share:
11,152
HappyHands31
Author by

HappyHands31

UX web designer, frontend developer, HTML, CSS, JavaScript, Python, C#, PHP, WordPress, and Adobe Creative Suite experience. gatewaywebdesign.com.

Updated on June 04, 2022

Comments

  • HappyHands31
    HappyHands31 almost 2 years

    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

  • HappyHands31
    HappyHands31 almost 7 years
    This did change the body for the default home page. So how would I get rid of the (right side) links in the header? i.imgur.com/zy070R2.png I.e. how do I change the header for the new default view?
  • Hossein Narimani Rad
    Hossein Narimani Rad almost 7 years
    They are in the _Layout.cshtml file. look at this part <div class="navbar navbar-inverse navbar-fixed-top">
  • Hossein Narimani Rad
    Hossein Narimani Rad almost 7 years
    Yes. That's because _Layout.cshtml is the default layout. If you don't like it you can change it as others said in their comments and also I'm going to update my answer.