Asp.net mvc 为什么Response.Write呈现,而Html.ActionLink不呈现?
Asp.net Mvc1 在我的Asp.net mvc 为什么Response.Write呈现,而Html.ActionLink不呈现?,asp.net-mvc,Asp.net Mvc,Asp.net Mvc1 在我的视图/home/Index.aspx上从http://localhost/DefectSeverityAssessmentMvcBeta/ 这使得 Response.Write("<a href=\""); Response.Write(Url.Action("Create", "Registration")); Response.Write("\">Begin Registration</a>"); 我有一个Registra
视图/home/Index.aspx上
从http://localhost/DefectSeverityAssessmentMvcBeta/
这使得
Response.Write("<a href=\"");
Response.Write(Url.Action("Create", "Registration"));
Response.Write("\">Begin Registration</a>");
我有一个RegistrationController和一个/Views/Registration/Create.aspx
注册控制器在Index()和Create()上有断点,但未命中它们
我不确定在此场景中如何使用
,因为它位于以下代码块中:
<% if (ViewData.ContainsKey("user"))
{
if (ViewData.ContainsKey("registered") && (bool)ViewData["registered"] == true)
{
//Html.RouteLink("Resume Assessment", "Assessment", new { controller = "Assessment", action = "Index" });
Response.Write("<a href=\"");
// Html.ActionLink("Resume Assessment", "Index", "Assessment");
Response.Write("\">Resume Assessment</a>");
}
else
{
//Html.RouteLink("Begin", "Registration", new { controller = "Registration", action = "Edit" });
// Html.ActionLink("Begin Registration", "Create", "Registration");
Html.RouteLink("Begin", "Default", new { controller = "Registration", action = "Edit" });
//Response.Write("<a href=\"");
//Response.Write(Url.Action("Create", "Registration"));
//Response.Write("\">Begin Registration</a>");
}
}
else
{ Response.Write("Authentication failed"); }
%>
您是否在上下文切换中使用等号,如下所示
<%= Html.ActionLink("Begin Registration", "Create", "Registration"); %>
^--needs equals sign here
^--这里需要等号
如果不使用等号,则必须直接写入响应对象
至于路由错误,您可以使用Phil Haack的诊断工具检查您的路由
对于路由,任何小于IIS7的内容。您是否在HTML中同时使用Response.Write和HTML.ActionLink?尝试使用for Html.ActionLink(…)
添加的等号调用响应。在后台写入,从而将代码写入屏幕。因为Html.ActionLink返回字符串而不写入响应流。您需要使用
或Response.write()写入页面代码>
我没有利用这个能力
<% if(x) {%> <%=Html.ActionLink(...)%><% } %>
多亏了查尔斯·康威,我才让它运转起来。以下是我总结的代码:
<div class="entry">
<% if (ViewData.ContainsKey("user"))
{
if (ViewData.ContainsKey("registered") && (bool)ViewData["registered"] == true)
{ %>
<%=Html.ActionLink("Resume Assessment", "Index", "Assessment") %>
<% }
else
{ %> <%=Html.ActionLink("Begin Registration", "Create", "Registration") %>
<%
}
}
else
{ Response.Write("Authentication failed"); }
%></div>
我以为=符号表示这是一个表达式。你不能在那里使用If语句和代码块,对吗?+1这个答案很有用,我不知道答案。在幕后写。你可以将If语句和实际输出用+1包装到注释中,我不能再次+1答案,但我接受了。你的评论是我遗漏的部分。我添加了一个答案,并在代码中实现了您的解决方案。有趣的是,他说第一种方法会产生一个返回404的链接。我想知道这个路由是否存在?这不应该属于默认路由吗?它的形式是/controller/action,那么您应该只使用Response.Write。404听起来像是一个路由问题。我该如何诊断路由问题?您可以使用Phil Haack的诊断工具查看您的路由,尽管这看起来很奇怪,因为Url.Action应该从路由表返回一个有效的Url链接。我在IIS 5上与此有关吗?在家里,我添加了。*作为指向asp.net IIS dll的内容,但我不记得是什么。
<% if(x) {%> <%=Html.ActionLink(...)%><% } %>
<div class="entry">
<% if (ViewData.ContainsKey("user"))
{
if (ViewData.ContainsKey("registered") && (bool)ViewData["registered"] == true)
{ %>
<%=Html.ActionLink("Resume Assessment", "Index", "Assessment") %>
<% }
else
{ %> <%=Html.ActionLink("Begin Registration", "Create", "Registration") %>
<%
}
}
else
{ Response.Write("Authentication failed"); }
%></div>