tags:

views:

179

answers:

4

Asp.net Mvc1 On my Views/home/Index.aspx which routes from http://localhost/DefectSeverityAssessmentMvcBeta/

This renders

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

But returns a 404 for the address of the link http://localhost/DefectSeverityAssessmentMvcBeta/Registration/Create

while this Does not render or show in view source but doesn't cause any exception:

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

I have a RegistrationController and a /Views/Registration/Create.aspx The registration controller has breakpoints on Index() and Create() but they are not being hit.

I'm not sure how I would use <%= %> in this scenario because it's inside the following code block:

<% 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"); }
             %>
A: 

Are you using an equals sign in your context switch, like this?

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

If you're not using an equals sign, you have to write directly to the Response object.

As far as the routing errors go, you can check out your routes using Phil Haack's diagnostic tool at http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx.

Anything less than IIS7 requires special configuration for the routing.

Robert Harvey
I updated the question to include why I'm not using the `=`
Maslow
Then you should just use Response.Write. The 404 sounds like a routing problem.
Robert Harvey
How would I diagnose the routing problem?
Maslow
You can check out your routes using Phil Haack's diagnostic tool at http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
Robert Harvey
It seems odd, though, because Url.Action should be returning a valid URL link from the routing table.
Robert Harvey
Would me being on IIS 5 have anything to do with it? At home I added .* as pointing to the asp.net IIS dll for something but I don't remember what.
Maslow
Anything less than IIS7 requires special configuration for the routing. See http://www.codedigest.com/Articles/ASPNETMVC/216_Hosting_and_Deployment_of_an_ASPNET_MVC_Application_in_IIS6_and_IIS7.aspx
Robert Harvey
It does appear I needed to do the special configuration to solve the 404.
Maslow
+2  A: 

Are you using <% %> in the HTML for both Response.Write and Html.ActionLink? Try using <%= %> for Html.ActionLink(...);

The added equal sign calls Response.Write behind the scenes thus writing you code to the screen.

Chuck Conway
I thought the = sign meant it was an expression. and you can't use If statements and code blocks in there right?
Maslow
+1 this answer is helpful, I didn't know about the Response.write behind the scenes.
Maslow
You can wrap the if statements in <% %> and the wrap the actually output in <%= %>
Chuck Conway
+1 to the comment, I can't +1 the answer again but I did accept it. Your comment was the parts I was missing. I added an answer with your solution implemented in my code.
Maslow
+2  A: 

Because Html.ActionLink return the string and do not write to the response stream. You need to write to your page using <%= %> or Response.Write();

Response.Write(Html.ActionLink("Begin Registration", "Create", "Registration"));
Gregoire
A: 

I was not making use of the ability to use

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

Thanks to Charles Conway I got it working. Here's the code I wound up with:

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