简体   繁体   中英

ASP.NET MVC5: Directly view the .cshtml file in browser

I am new in ASP.NET MVC, I am trying to open Navbar.cshtml in browser directly. After a lots of efforts and seeing results I am not able to open it directly. It is showing me same errors after testing many results.

在此处输入图片说明

My Navbar.cshtml file is in view folder directory.

Navbar.cshtml

@{
    ViewBag.Title = "Navbar";
}

<h2>Navbar</h2>

Like others have stated, you won't be able to view .cshtml files directly and need a controller to handle it. Doing something like this should help you view your desired Navbar.cshtml.

If possible, you should also set the correct startup file in the Startup.cs file.

First of all you have to create a controller action associate with that view.

In your controller,create an action method like this

 public ActionResult Navbar()
    {
       //do something
    }

Right click on actionname Navbar,add view Navbar.cshtml. Then run your cshtml file.

I think this is the solution you are looking . Lets say your Navbar.cshtml file is located under Shared folder

View("~/Views/Shared/Navbar.cshtml");

And also you have to call all view from a controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM