2

Is there an alternative method to using strings for action and controler names as string?

for example is there any method similar to

typeOf(MyWebSite.Controllers.ControllerZ.Action01).Name
typeOf(MyWebSite.Controllers.ControllerZ).Name

being used? or is there a way to just use the Controller method name as such : MyWebSite.Controllers.ControllerZ.Action01 as a parameter so there is no need to also hardcode a string for Controller name that is now deriveable from Action?

Forexample consider Html.BeginForm("actionname","controllername"), how do we get rid of hardcoded strings in that?

jimjim
  • 2,414
  • 2
  • 26
  • 46
  • Please can you let us know what the problem is that you're facing – Luke Sep 20 '16 at 08:53
  • Do you mean to achive something like: @Url.Action(x => x.Index) and this to generate url t0 HomeController -> Index action method? – ssimeonov Sep 20 '16 at 08:55
  • @Luke want to use strongly typed redeemed than hard doing strings. I am sure I can't be the first person who doesn't like hardicoding strings instead of using strong typing – jimjim Sep 20 '16 at 08:56
  • 2
    One of the solutions is T4MVC - http://stackoverflow.com/questions/8371103/magic-strings-in-asp-net-mvc/8371138#8371138. – kiziu Sep 20 '16 at 08:56
  • @media, what does it have to do with a problem? Did you even read it? – kiziu Sep 20 '16 at 09:08
  • 2
    C# 6 has nameof – Atomosk Sep 20 '16 at 09:12
  • @Atomosk, that won't work as easily as it should - `nameof` will include `Controller` in the name of controller, which helpers do not accept. – kiziu Sep 20 '16 at 09:19
  • http://stackoverflow.com/questions/18216413/what-is-the-syntax-for-a-strongly-typed-actionlink-in-mvc-4-with-mvc-4-futures – CodeCaster Sep 20 '16 at 09:24
  • 1
    MVC magic name strings are going to bite you on this. The class/type name does not line up with what the helpers and even controller DI expects (x-'controller'). – Paul Swetz Sep 20 '16 at 13:53

0 Answers0