1

I want to generate constant urls for my actions. Something like this:

public const string ControllerName_ActionName = "/ControllerName/ActionName";

I have tried to use T4MVC but it generates only methods with T4MVC_System_Web_Mvc_ActionResult. Is there any solution without pain?

I want to use them in an external project as a library. The project doesn't know about my MVC application. It can be a simple console application with WebClient.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Neshta
  • 2,605
  • 2
  • 27
  • 45

2 Answers2

0

From within the web application

T4MVC also generates a new extension method for Url.Action(). So for instance now you can use something like this:

return Redirect(Url.Action(MVC.Account.Index()));

From an external library

T4MVC only generates constants so you can be certain that when it compiles the routes will be correctly generated runtime. It is not easy to generate a file containing something like url => Controller.Action because this depends on your routing config, url wildcards, ordering, http protocol etc.

If your project has no knowlegde of the MVC application and you want to have a file containing the constant urls it can hit how would this file be generated if your route is the following:

      routes.MapRoute(
          name: "Catch-all content route",
          url: "{*url}",
          defaults: new { controller = "ContentPage", action = "Index" }
Mark van Straten
  • 9,287
  • 3
  • 38
  • 57
  • 1
    And on top, T4MVC has that thing called a configuration file - you can customize a LOT of what and how it is generated. – TomTom Dec 10 '14 at 10:01
  • I want to use the constants in an external project as a library. The project does't know about my MVC application. It can be a simple console application with WebClient. – Neshta Dec 10 '14 at 10:02
  • @Neshta the only thing making your console application violate the HATEOS constraint gives you is more brittleness. Your MVC application can know about your MVC application, your client can know the URI of a indexing page that tells it the URIs on start up. One extra web request on client startup solves this problem and also gives you a lot of scope for future changes. – Jon Hanna Dec 10 '14 at 10:32
0

I have customized T4MVC template for my issue:

public static class UrlConst {
<#foreach (var controller in DefaultArea.GetControllers()) {
    foreach (var action in controller.ActionMethods.ToList())
    { #>
    public const string <#=controller.Name#>_<#=action.ActionName#> =
        "/<#=controller.Name#>/<#=action.ActionName#>";
  <#}   
} #>
}
Neshta
  • 2,605
  • 2
  • 27
  • 45