0

How to use hyper linking in Razor without @Url.* or @Html.*

There are 4 ways to hyperly link views in ASP.NET MVC as far as I know:

  • @Url.Action
  • @Url.ActionLink
  • @Html.Action
  • @Html.ActionLink

I can use them as, for example:

<a href=@Url.Action("ActionName","ControllerName")> Test </a>

Which generates:

<a href="ControllerName/ActionName"> Test </a>

Is there a way to generate the Url part i.e. "ControllerName/ActionName" in ASP.NET MVC Razor apart from the above mentioned 4 ways?

Background: Mono is not able to recognize these kind of Razor components. If some kind of Url generation works out then we can use ASP.NET MVC 5 with Razor on Mono.

Community
  • 1
  • 1
Zameer Ansari
  • 28,977
  • 24
  • 140
  • 219
  • All Url generation code goes through the same codepath at the end, so I doubt any existing methods using Route Table would work...If you can't rebuild Mono - Consider reading the code and building your own helper that builds Url based on routing table/attributes. – Alexei Levenkov May 31 '15 at 17:09
  • You right @AlexeiLevenkov. I have the same doubt, but I just wanted to give it a try. Maybe if it works out. The last resort would be to make some kind of helper. – Zameer Ansari May 31 '15 at 17:14
  • also you always can just write url without helpers. – teo van kot May 31 '15 at 17:18
  • @AlexeiLevenkov - Hey, you're a MS guy!!! Just one question - is MS billing Xamarin for making Mono ASP.NET MVC work on Linux. Mono MVC works best on Windows but on Linux it keep failing. What are MS's plans for it? – Zameer Ansari May 31 '15 at 17:19
  • @teovankot - but how can I refer to an Action in a Controller? in Webforms it is pretty straightforward. e.g: ` Test ` – Zameer Ansari May 31 '15 at 17:21
  • 2
    @zameeramir you already write it =) `href="ControllerName/ActionName"` that's how you reffer – teo van kot May 31 '15 at 17:23
  • @teovankot - many +1s for opening my eyes. But after clicking the anchor it demonstrates some strange behavior. I'm updating the question. – Zameer Ansari May 31 '15 at 17:28
  • 1
    @zameeramir I have as much information as you can get about plans of other teams - whatever is publicly available on blogs :). (I'm also not particularly involved with Xamarin, so I can't even recommend possible good source of info you are looking for). – Alexei Levenkov May 31 '15 at 17:31
  • There is no such method `@Url.ActionLink()` and `@Html.Action()` does not create a link (it calls a controller method to render a partial view) –  May 31 '15 at 23:00

1 Answers1

2

Well, you can always look up ASP.NET MVC source code, and copy what you need, if it is not implemented in mono yet. Here you can find it: https://github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/System.Web.Mvc/Html/LinkExtensions.cs

But I've been using mono and ASP.NET MVC 4, and I had no problem like you. Here is my code on github, it was running on mono and xsp. https://github.com/rstraszewski/HomeCenter/tree/master/HomeCenter.Presentation

As you can see, for example in _Layout.cshtml, I was using Html.ActionLink...Although I was using windows and visual studio for compiling and developing, and mono only for runtime...

You can also create your own html helpers to generate links... And there is also a Html.RouteLink helper method, which also can generate you a link.

Example of using Html.RouteLink:

@Html.RouteLink("Some link", "Default", new RouteValueDictionary(new {controller = "Home", action = "Index"}))

Yes, you can also use something like:

<a href="~/Home/Index">Some link</a>

Notice that the ~/ is important in the above snippet, without it the Url demonstrates a strange behaviour but you need to remember, that this will work, because this is how default routing is working... If you change default routing, this can not work.

Zameer Ansari
  • 28,977
  • 24
  • 140
  • 219
Rafał Straszewski
  • 960
  • 11
  • 10