我已将.cshtml
网站上传到surftown
服务器,我在运行代码时遇到了一些问题。但我在运行Razor
代码时遇到问题。
这是页面呈现的方式:(Default.cshtml)
我已经检查了内部通信问题。这是我的结果:
但是为什么它不起作用,我该如何解决呢?
我听说这可能是观点的问题,但如果是这样的话我该如何解决?
我的网站文件夹树:(以及一些文件)
我的web.config文件如下所示:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0">
<buildProviders>
<add extension=".cshtml" type="System.Web.WebPages.Razor.RazorBuildProvider, System.Web.WebPages.Razor"/>
</buildProviders>
<assemblies>
<add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</assemblies>
</compilation>
</system.web>
<connectionStrings>
<add connectionString="database connection" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
编辑:
问题是我的所有文件都是.cshtml
吗?并将它们添加为MIME Type
html/text
。
更多编辑: 这是我的Default.cshtml代码:
@{
Layout = "~/Views/_Layout.cshtml";
}
<table>
<tr>
<td>
<table>
<tr>
<td>
<h1 class="header">
Welcome to the library
</h1>
</td>
</tr>
<tr>
<td>
<p>
Do look around, to find information about almost anything there is to know about Mythodea, or maybe you are here to make a chapter or note of your own.
</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
我的_Layout.cshtml代码: @ { //一堆C#代码 }
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link href="~/Styles/CSS/Layout.css" rel="stylesheet" />
<script src="~/Scripts/Addons/TinyMCE/tinymce.min.js"></script>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
@RenderSection("head",false)
<title>The Kettle Library of Mythodea</title>
</head>
<body>
<div id="container">
<div id="header">
<h1>
The Library
</h1>
</div>
<div id="left_column">
<div class="innerPadding">
<div id="Menu_Overview">
@Html.Raw(menu)
</div>
</div>
</div>
<div id="right_column">
<div class="innerPadding">
@{
if(!SessionHandler.UserIsLoggedIn)
{
<div class="Login_Wrapper">
<form action="" method="post">
<table>
<tr>
<td>
<p>
Username/Email:
</p>
</td>
<td>
<input class="form_input" type="text" placeholder="Username / Email" tabindex="1" name="username" value="@username" />
</td>
</tr>
<tr>
<td>
<p>
Password:
</p>
</td>
<td>
<input class="form_input" type="password" placeholder="Password" tabindex="2" name="password" />
<a href="#">forgot?</a>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Login" name="btn" />
</td>
<td>
<input type="submit" value="Register" name="btn" />
</td>
</tr>
</table>
</form>
<p class="Error_Message">@message_error</p>
</div>
}
else
{
<div class="Login_Wrapper">
<form action="" method="post">
<p>
@name
</p>
<input type="submit" value="Logout" name="btn" />
</form>
</div>
}
}
<div class="SideMenu_Right">
<div id="search_wrapper">
<form action="" method="post">
<input type="text" name="searchField" placeholder="Search" value="@searchValue" required="required" />
<input type="submit" name="btn" value="Search" />
</form>
</div>
@{
if(SessionHandler.UserIsLoggedIn)
{
<a href="~/Pages/Entries/EnterEntry.cshtml">Create new Chapter</a>
<br />
<br />
}
}
@RenderSection("menu_right", false)
</div>
</div>
</div>
<div id="middle_column">
<div class="innerPadding">
@RenderBody()
</div>
</div>
</div>
<div id="madeBy">
<p>
Created by <a href="https://www.facebook.com/michaeltot.korsgaard" target="_blank">Michael Tot Korsgaard</a>
</p>
</div>
</body>
</html>
答案 0 :(得分:4)
您似乎没有使用MVC架构。为什么使用Pages
而不是Views
?
我已经看到了你所有的错误,而且每件事情都正常。以这种方式解决部署问题太难了。所以我在实际服务器上部署MVC应用程序的基本指令可能很有用:
到目前为止,您已经配置了使用Windows服务器的应用程序,虽然它不支持MVC技术,但您的应用程序具有相关的程序集引用以在服务器上运行/编译。现在所有事情都与服务器和IIS配置有关。请按照以下说明操作:
修改强>
关于MVC中的默认页面呈现,请考虑在 Global.asax 文件中我们设置默认路由,如bellow(通过调用RegisterRoutes
类RouteConfig
类的函数):
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
在MVC架构中,我们不使用传统的ASP页面路由,例如使用 Default.cshtml 。 每个Controller根据其操作方法调用Views Folder中的同名文件夹并控制.cshtml子页面。没有选择可以重命名放置在应用程序上的Default View文件夹。 关于Mastering页面考虑默认情况下每个MVC应用程序在决定渲染视图时,首先调用位于根文件夹的 _ViewStart.cshtml 文件,以了解母版页的名称是什么根据_ViewStart的内容。在此步骤到下一步,我们有主视图和部分视图概念。
例如,如果您尝试更改_ViewStart.cshtml文件的名称(例如_Start.cshtml),您将在浏览器中看到以下页面:
因此,在使用MVC架构时,您必须遵循一些规则/合同来构建成功的应用程序。
答案 1 :(得分:1)
您缺少来自Views / Pages文件夹的web.config
它应该是这样的:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
</system.web.webPages.razor>
<appSettings>
<add key="webpages:Enabled" value="false" />
</appSettings>
<system.web>
<httpHandlers>
<add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>
<!--
Enabling request validation in view pages would cause validation to occur
after the input has already been processed by the controller. By default
MVC performs request validation before a controller processes the input.
To change this behavior apply the ValidateInputAttribute to a
controller or action.
-->
<pages
validateRequest="false"
pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<controls>
<add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
</controls>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
</configuration>