asp.net-mvc - 为什么 Response.Write 会渲染而 Html.ActionLink 不会?

标签 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>");

但对于链接地址http://localhost/DefectSeverityAssessmentMvcBeta/Registration/Create返回404

虽然这不会在 View 源中渲染或显示,但不会导致任何异常:

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

我有一个 RegistrationController 和一个 /Views/Registration/Create.aspx 注册 Controller 在 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 中为 Response.Write 和 Html.ActionLink 使用 <% %>?尝试使用 <%= %> 表示 Html.ActionLink(...);

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

关于asp.net-mvc - 为什么 Response.Write 会渲染而 Html.ActionLink 不会?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1798281/

相关文章:

javascript - 为什么 Ajax 调用不将操作中的数据返回到成功回调函数?

javascript - 使用 JavaScript 将多个值附加到 <table>

asp.net-mvc - 从 View 页面删除操作

asp.net-mvc - 具有空模型的 renderpartial 传递了错误的类型

asp.net-mvc - 维护ASP.NET MVC中复选框动态列表的状态

c# - 找不到 Visual Studio 2015 上的 ASP.NET 5 addMvc 方法

asp.net-mvc - 如何使用 Gulp 高效管理和处理 Bower 包? (VS2015,Visual Studio 2015)

c# - 在 ActionResult 中使用 ToList ForEach 更新表值

c# - 如何将行号添加到剑道 ui 网格?

c# - 使用 MVC AccountController 登录 Android App