.CSHTML pages will not render

天涯浪子 提交于 2019-12-22 08:44:43

问题


I am trying to get my server to run .cshtml files. Using WebMatrix 3 I can view the pages by right clicking and selecting "view in browser", but that views through a localmachine port. If I try to access the pages from the internet, I get a 500 error.

Here is what I have done:

  • I made sure that MVC 3 was installed (but I keep reading that MVC is not needed for .cshtml... I made sure anywway.
  • I did the whole "revert to parent" / "revert to inherited" thing, which is found in many blogs.
  • I was getting a 404 error for a while, so I added a MIME type of "text/html" to .cshtml extension. That solved that problem, but only led to my 500 error.

I am suspecting it's something to do with my web.config file because if I remove the file, the page will show, but it reads like text. For example (in the most basic page I could think of):

@{
   var currentTime = dateTime.Now; 
}

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Testing</title>
        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    </head>
    <body>
        The current time is @currentTime .
    </body>
</html>

The page will render like:

@{ var currentTime = dateTime.Now;} The current time is @currentTime .

Yet, I am clueless as to what should or should not be in the web.config file. I have been wracking my brain over this for close to a month...

Here is what is in the web.config file:

<configuration>     

    <system.data>
        <DbProviderFactories>
            <remove invariant="System.Data.SqlServerCe.4.0" />
            <add invariant="System.Data.SqlServerCe.4.0" name="Microsoft® SQL Server® Compact 4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
        </DbProviderFactories>
    </system.data>
    <appSettings>
        <add key="webPages:Version" value="2.0"/>
        <add key="webpages:Enabled" value="true" />
    </appSettings>

    <system.webServer>
        <httpErrors errorMode="Detailed" />
    </system.webServer>

</configuration>

Many thanks in advance.


回答1:


If I understand you correctly you are having problems running this in IIS (Internet Information Services - the built in Web server on Windows). It looks like the directory you are serving is not configured as a Web Application (but it is configured as a Virtual Directory). To fix this:

  1. Open IIS manager (Win + R and copy %systemroot%\system32\inetsrv\iis.msc to the run window)
  2. Navigate to your directory in the tree on the left (probably \Sites\Default Web Site\something
  3. Right click on it and choose "Convert to Application"

These steps should make IIS at least try to render the .cshtml.

On a separate note .cshtml (ie ASP.NET MVC) is so much more easier to learn and understand than .asp (ie simply ASP.NET)




回答2:


It seems that I had an older version... this was answered here:

CSHTML rendering text only - static page?



来源:https://stackoverflow.com/questions/18193735/cshtml-pages-will-not-render

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!