Paul
Paul

Reputation: 1159

Razor for ASP.NET WebPages and IIS

I'm trying out ASP.NET webpages using the razor syntax so I created a sample site. If I view the site in WebMatrix or using VisualStudio (view in browser) everything works fine. When I try to create an IIS site I get a 404 error when trying to access any .cshtml files. Regular html files work from the site. It's set to .net 4 integrated. I googled for an hour with no luck.

Upvotes: 1

Views: 9853

Answers (4)

Matt Kemp
Matt Kemp

Reputation: 2920

This is a top hit for razor pages not working for .NET core or .NET 5 sites too.

I like to run my local development in IIS as it mirrors production. When setting up a new project, I set it up in IIS like normal but get a 404. All prerequisites are installed, I even have other .NET 5 razorpage sites running, so know it's a project setting.

Fix for me is to edit Properties\launchSettings.json (or edit via the GUI Project > ProjectNameHere Properties > Debug tab)

remove all profiles except one named after my project "ProjectNameHere" Have the following settings:

  "commandName": "IIS",
  "launchBrowser": false,
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development",
    "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
  },
  "dotnetRunMessages": "true",
  "applicationUrl": "https://dev.projectnamehere.com"
  1. Then save and press F5 or the Debug button in the toolbar.
  2. Website will now work on https://dev.projectnamehere.com
  3. Stop debugging
  4. Hit https://dev.projectnamehere.com like a normal site
  5. make razor page changes, refresh browser page like normal, see changes

Note: I edit my C:\Windows\System32\drivers\etc\hosts file to add 127.0.0.1 dev.projectnamehere.com and use an online tool to create a self signed TLS cert for the domain which I install, to get all that working like production.

Upvotes: 0

William Jens
William Jens

Reputation: 442

Check to see if ".cshtml" is mapped correctly within the site's

Properties -> Home Directory -> Configuration... -> Mappings

Mine is mapped to:

c:\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll

Upvotes: 0

Neil Knight
Neil Knight

Reputation: 48547

We had exactly the same problem at my company. To get it to work, we had to do a full .Net 4 install and the pages are being served correctly.

Upvotes: 0

Codo
Codo

Reputation: 78825

Could it be that you have just copied the files of your MVC application into a directory (or virtual directory) of IIS?

In order for your ASP.NET MVC application to work, you need to configure a web application in the IIS manager (right-click on "Default Web Site" and choose "Add application"). Then deploy your application to the just created application directory.

Upvotes: 1

Related Questions