1

I have a problem when trying to generate url in the controller constructor

    private BreadCrumbItem breadCrumbItem;

    public SummaryController()
    {
        this.breadCrumbItem = new BreadCrumbItem { Title = "Sommaire", Url = Url.RouteUrl(new { controller = "Summary", action = "Index" }) };

    }

The problem is in Url.RouteUrl

Why i can't access this in the controller? Any way to fix this ? Beacause otherwise i have to add the same code in all actions in this controller.

Thanks for help

riadh gomri
  • 869
  • 1
  • 15
  • 21

4 Answers4

5

If i understand your question right you want something like this:

public class SummaryController
{
    public SummaryController()
    {

    }

    private BreadCrumbItem _breadCrumbItem = null;

    private BreadCrumbItem CrumbItem
    {
        get
        {
             if(_breadCrumbItem == null)
             {
                  _breadCrumbItem = new BreadCrumbItem { Title = "Sommaire", Url =        Url.RouteUrl(new { controller = "Summary", action = "Index" }) };
             }

             return _breadCrumbItem;
         }
    }
}

Now in each method you can simply use the CrumbItem and the first time it'll create the new BreadCrumItem and after that it'll just return you the created item each time it's called.

middelpat
  • 2,555
  • 1
  • 20
  • 29
  • Very good idea, but i need to do different logic that depends on wich action is called – riadh gomri Nov 21 '12 at 14:19
  • But if you need to change the parameters in each action, then you'll need to call or just modify this data in each action. you could then just write a method that takes a few parameters and builds this BreadCrumItem for you. but since it's a really small peace of code that wouldn't be necessary – middelpat Nov 21 '12 at 14:28
  • Is `CrumbItem` supposed to be a property? you are missing the `get` block. – Dennis May 08 '13 at 08:08
3

You can override the Initialize() method. The Url property is set once the base controller is initialized.

protected override void Initialize(RequestContext requestContext)
{
    // the controller's UrlHelper is still null
    base.Initialize(requestContext);

    // the controller's UrlHelper is now ready to use!
    var url = Url.RouteUrl(...);
}
djs
  • 1,660
  • 1
  • 17
  • 35
2

You can access it in the controller but you can't access it in the constructor. This value isn't set at the time the constructor is called, obviously, because it it populated after the controller is created by the controller builder. Using the @middelpat solution to lazy load the property (creating it on first use in the current action) is a reasonable solution to the problem. The UrlHelper instance should be available in the controller at that point.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • Thank's for the answer, any documentation on ASP.NET MVC page lifecycle will be apreciated. – riadh gomri Nov 21 '12 at 14:21
  • @riadh_vmc http://stackoverflow.com/questions/460145/what-is-the-page-lifecycle-of-an-asp-net-mvc-page-compared-to-asp-net-webform Also, it's just plain common sense that an instance property wouldn't be set in the constructor unless you set it yourself. MVC isn't magic, even the framework has to call the class constructor before it can set any instance properties. – tvanfosson Nov 21 '12 at 16:38
0

I think you need something like this answer:

https://stackoverflow.com/a/700357/637425

Quote:

If you just want to get the path to a certain action, use UrlHelper:
UrlHelper u = new UrlHelper(this.ControllerContext.RequestContext);
string url = u.Action("About", "Home", null); 
if you want to create a hyperlink:
string link = HtmlHelper.GenerateLink(this.ControllerContext.RequestContext,
System.Web.Routing.RouteTable.Routes, "My link", "Root", "About",
"Home", null, null);  
Intellisense will give you the meaning of each of the parameters.
Community
  • 1
  • 1
SynerCoder
  • 12,493
  • 4
  • 47
  • 78