1

How to generate a URL for ASP.NET MVC without string names? I.e. I have Index action in HomeController. Using strings I would go for

@Url.Action("Index","Home")

but I would like to avoid use of string. I remember seeing a new way how to do this without strings, but I cannot find it anywhere now. The only code that I have found was

@(Url.Action<HomeController>(x=>x.Index()))

but that does not work for me. Error given is

The non-generic method 'System.Web.Mvc.UrlHelper.Action()' cannot be used with type arguments.

My ASP.NET MVC version is 5.2.3. If the above is only for 6+, is there any way to make achieve what I want?

3
  • 2
    The answer is no and what is wrong with the strings??? Commented Jan 19, 2016 at 14:48
  • 2
    Does not survive refactoring, thought that would be obvious.... Also, it would be nice if you did not downvote questions you do not understand:) Commented Jan 19, 2016 at 14:48
  • 1
    Oh really? Can you explain how the code you trying to come up with would help in terms of refactoring? Because to me you are just trying to re-invent the wheel which is completely a waste of time. KISS! Commented Jan 19, 2016 at 15:08

2 Answers 2

2

You might want to check out T4MVC: https://github.com/T4MVC/T4MVC

Sign up to request clarification or add additional context in comments.

Comments

1

You need a nuget package for that:https://www.nuget.org/packages/Microsoft.AspNet.Mvc.Futures/

this way you can do things like this:

<%= Html.ActionLink<MyController>(x => x.MyMethod(a), "text") %>
<%= Html.BuildUrlFromExpression<MyController>(x => x.MyMethod(a)) %>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.