适用于 ASP.NET 网页和 IIS 的 Razor
我正在尝试使用 razor 语法的 ASP.NET 网页,因此我创建了一个示例站点。如果我在 WebMatrix 中或使用 VisualStudio(在浏览器中查看)查看该网站,一切正常。当我尝试创建 IIS 站点时,在尝试访问任何 .cshtml 文件时收到 404 错误。常规 html 文件可以在网站上使用。它设置为集成.net 4。我用谷歌搜索了一个小时,但没有运气。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
难道您刚刚将 MVC 应用程序的文件复制到 IIS 的目录(或虚拟目录)中?
为了使 ASP.NET MVC 应用程序正常工作,您需要在 IIS 管理器中配置 Web 应用程序(右键单击“默认网站”并选择“添加应用程序”)。然后将您的应用程序部署到刚刚创建的应用程序目录。
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.
我们在我的公司也遇到了完全相同的问题。为了使其正常工作,我们必须进行完整的 .Net 4 安装并且页面可以正确提供。
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.
检查“.cshtml”是否在站点的
属性 -> 中正确映射主目录 ->配置...->映射
我的映射到:
c:\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll
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
对于不适用于 .NET core 或 .NET 5 站点的 Razor 页面来说,这是最热门的。
我喜欢在 IIS 中运行本地开发,因为它反映了生产环境。设置新项目时,我像平常一样在 IIS 中设置它,但收到 404。所有先决条件都已安装,我什至正在运行其他 .NET 5 razorpage 站点,所以知道这是一个项目设置。
对我来说,修复方法是编辑 Properties\launchSettings.json (或通过 GUI Project > ProjectNameHere Properties > Debug 选项卡进行编辑)
删除除以我的项目“ProjectNameHere”命名的配置文件外的所有配置文件
进行以下设置:
注意:我编辑我的C:\Windows\System32\drivers\etc\hosts 文件添加
127.0.0.1 dev.projectnamehere.com
并使用在线工具为我安装的域创建自签名 TLS 证书,以让所有这些像生产一样工作。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:
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.