2009-11-25 63 views
2

Asp.net MVC1 在我Views/home/Index.aspxhttp://localhost/DefectSeverityAssessmentMvcBeta/爲什麼Response.Write呈現但Html.ActionLink不?

路線這使得

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

但對於鏈接http://localhost/DefectSeverityAssessmentMvcBeta/Registration/Create

的地址返回一個404雖然這並不呈現或顯示查看源代碼但不會導致任何異常:

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"); } 
      %> 
+0

有趣的是,他說第一種方式產生的鏈接返回404。我不知道路線是否存在? – 2009-11-25 16:53:16

+0

不應該屬於默認路由嗎?它的形式/控制器/行動 – Maslow 2009-11-25 16:56:07

回答

4

您是否在HTML中使用了Response.Write和Html.ActionLink?< %%>嘗試使用<%=%>作爲Html.ActionLink(...);

添加的等號後面的代碼調用Response.Write,從而將代碼寫入屏幕。

+0

我認爲=符號意味着它是一個表達式。你不能在那裏使用If語句和代碼塊嗎? – Maslow 2009-11-25 16:51:50

+0

+1這個答案很有幫助,我不知道幕後的Response.write。 – Maslow 2009-11-25 17:02:52

+1

您可以將if語句包裝在<% %>中,並將<%= %> – 2009-11-25 18:36:58

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的路由。

+0

我更新了問題,以包括爲什麼我不使用'=' – Maslow 2009-11-25 16:52:34

+0

然後你應該只使用Response.Write。 404聽起來像一個路由問題。 – 2009-11-25 16:54:20

+0

我將如何診斷路由問題? – Maslow 2009-11-25 16:55:00

2

因爲Html.ActionLink返回字符串,不寫入響應流。你需要寫信給你的頁面使用<%= %>Response.Write();

Response.Write(Html.ActionLink("Begin Registration", "Create", "Registration")); 
0

我沒有利用的能力,使用

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