为什么Response.Write呈现但Html.ActionLink不呈现?

时间:2009-11-25 16:46:06

标签: asp.net-mvc

Asp.net Mvc1 在Views/home/Index.aspx来自http://localhost/DefectSeverityAssessmentMvcBeta/

的路线上

这呈现

  Response.Write("<a href=\"");
  Response.Write(Url.Action("Create", "Registration"));
  Response.Write("\">Begin Registration</a>");

但是返回404作为链接地址http://localhost/DefectSeverityAssessmentMvcBeta/Registration/Create

虽然这不会在视图源中呈现或显示但不会导致任何异常:

Html.ActionLink("Begin Registration", "Create", "Registration");

我有一个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"); }
             %>

4 个答案:

答案 0 :(得分:4)

您使用的是&lt; %%&gt;在Response.Write和Html.ActionLink的HTML中?尝试使用&lt;%=%&gt; for Html.ActionLink(...);

添加的等号在幕后调用Response.Write,从而将代码写入屏幕。

答案 1 :(得分:2)

因为Html.ActionLink返回字符串而不写入响应流。您需要使用<%= %>Response.Write();

写信给您的网页
Response.Write(Html.ActionLink("Begin Registration", "Create", "Registration"));

答案 2 :(得分:0)

你在上下文切换中使用等号吗?像这样?

<%= Html.ActionLink("Begin Registration", "Create", "Registration"); %>
  ^--needs equals sign here

如果您没有使用等号,则必须直接写入Response对象。

就路由错误而言,您可以使用Phil Haack的诊断工具http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx查看您的路线。

路由的任何小于IIS7 requires special configuration的内容。

答案 3 :(得分:0)

我没有使用

的能力
<% if(x) {%> <%=Html.ActionLink(...)%><% } %>

感谢Charles Conway我得到了它的工作。这是我最后编写的代码:

 <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>