0

How can I just write

Response.Write("<a href='/someurl'>Some Text</a>"); 

Instead of

Response.Write(Html.ActionLink(...)); 

This reason I am after this is because I am writing a Pagination ViewUserControl. In the control I want it to show page numbers, next and previous pages etc.

When I use the following code

Response.Write(Html.ActionLink(page.ToString(), HttpContext.Current.Request.RawUrl, new { page = page })); 

The link is written out as http://localhost:61293/customers/customers/System.Web.Mvc.UrlParameter/page2 which is obviously incorrect.

HttpContext.Current.Request.RawUrl == "/customers/" in this example. I would have expected that the resultant Url was then /customers/page2 as opposed to what is actually written out.

My routes are set up like so:

routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}/page{page}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional, page = UrlParameter.Optional } // Parameter defaults ); 

Can anyone shed some light?

4
  • 1
    Your question is very unclear. Where are you trying to do this and where do you want this link to get rendered? Why don't you want to use ActionLink? Why are you using Response.Write? Commented Dec 22, 2010 at 14:03
  • Thanks - I have updated question with the reasons why! Commented Dec 22, 2010 at 14:13
  • now that you have updated your question, you are saying that you are writing a view control. What exactly do you mean by this? Is it a class that derives from UserControl? Commented Dec 22, 2010 at 14:18
  • I use simply html instead of helper because in html code is easy to read and easy to know what i do. Commented Dec 22, 2010 at 14:18

4 Answers 4

1

Based on your update, you shouldn't be using HttpContext.Current.Request.RawUrl as the action of your ActionLink.

http://msdn.microsoft.com/en-us/library/dd505040(v=VS.90).aspx

public static string ActionLink( this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues ) 

As you can see, HttpContext.Current.Request.RawUrl will not match any of your actions. Try writing:

Html.ActionLink(page.ToString(), "TheNameOfTheActionMethod", new { page = page }); 
Sign up to request clarification or add additional context in comments.

11 Comments

I don't see how this is preferable over <%= Html.ActionLink("my link", "someaction") %>.
me neither, but it'll get you the url
I guess if you don't like passing in the html attributes or you want it to look more like HTML. I usually use ActionLink but to each its own.
@hunter, I think that the OP needs to explain what he is trying to do. Also the third example you've added with Response.Write, what a tag soup, I don't have words to describe it :-)
@Darin, agreed. Going off of his title I think this is what he's asking for.
|
0
Url.Action("ActionMethodName", new { page = i }); 

Comments

0

Chris,

By reading your "clarifications" and comments above I think you just overcomplicate things around.

Most clear thing that is recognizable between the lines is that you want somewhat reusable widget showing you the paginable list of customers.

So why not just simply:

  • Create CustomerController, and an Index() as an action to show list of customers
  • Map routes "customers" and "customers/page{page}" to CustomerController.Index()
  • Use Html.RenderAction("Index", "Customer", new {page = page}) wherever you need the list of customers to be rendered.

Following this way all the heavy lifting (including URL resolution) will be made for you by infrastructure and you'll get your "reusable widget" or "control".

As for mentioned HttpContext.Current.Request.RawUrl stuff - you have to decide: you either want to use ASP.NET routing or build all the routes by yourself. Because these techniques are somewhat excluding each other.

P.S. Anyway, more details on what exactly you want to achieve will help us to help you.

Comments

0

I think you have your routing set up improperly -- probably a parameter in the wrong place in one of your routes. The reason I say this is because it appears that the ToString method of the UrlParameter class itself is being called, resulting in the name of the class being output.

Paging Route (from comments) -- this assumes that the "list" action is "index" and you don't want the index action to appear in the route. Note that it inherits the controller from the current request and, since the action is by convention, you don't need to supply it. Completely untested.

routes.MapRoute( "Pager", // Route name "{controller}/page{page}", // URL with parameters new { controller = "Home", action = "Index", page = UrlParameter.Optional } // Parameter defaults ); 

Used as:

 <%= Html.RouteLink( page, "Pager", new { page = page }, null ) %> 

2 Comments

@Chris -- have you tried stripping off the slashes from the RawUrl? RawUrl.Trim('/').
@Chris - the problem may be that you don't have an id parameter in a route where the id parameter would be required. Try creating another route (I'll update my question with a sample) just for the pager without an id, then refer to it by name using the RouteLink extension. It should come after the default route.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.