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

0

4 Answers 4

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.

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

3 Comments

Very good idea, but i need to do different logic that depends on wich action is called
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
Is CrumbItem supposed to be a property? you are missing the get block.
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(...);
}

Comments

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.

2 Comments

Thank's for the answer, any documentation on ASP.NET MVC page lifecycle will be apreciated.
@riadh_vmc stackoverflow.com/questions/460145/… 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.
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.

2 Comments

this.ControllerContext is also null in the controller's constructor.
@djs Then use HttpContext.Current.Request.RequestContext to access the request context

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.