Legako
Legako

Reputation: 33

How to create a new .cshtml page in a project that can be seen through a URL

I am using Visual Studio to create a new website. I have a new page called test.cshtml with valid html inside in my solution files. I am trying to open it to debug in a browser, but every time I do it just shows a 404 error with no explanation as to what/why it is not being found. If I open it in Visual Studio view it still shows a 404 error never showing my html.

What do I have to do to create a new page I can navigate to from a browser if you don't just add an .cshtml page into the project files and navigate to that path in a browser?

Upvotes: 0

Views: 10586

Answers (1)

Troy Carlson
Troy Carlson

Reputation: 3121

In ASP.NET MVC, URLs map to controller actions, not files on the web server. MVC Views (.cshtml files) need to be rendered by the Razor engine. You need to create a controller action that renders your .cshtml view. In the example below, replace YourController with the appropriate name and replace YourAction with the name of your .cshtml view. Then you can navigate to this page by hitting yourdomain.com/YourController/YourAction.

public class YourController: Controller
{
    //
    // GET: /YourController/

    public ActionResult YourAction()
    {
        // Add action logic here
        return View("YourAction");
    }
}

More reading about ASP.NET MVC controllers/actions: http://www.asp.net/mvc/tutorials/controllers-and-routing/aspnet-mvc-controllers-overview-cs

Upvotes: 4

Related Questions