1

I am trying to use the following code to generate url string:

var x = @Url.Action("AddOrEditSaleOffer", "BuildingOffers", null);

However, the returned value somehow generates the the following url:

/BuildingOffers/AddOrEditSaleOffer/7

where 7 is a number that changes based on the id of the last edited Offer.

The code is in a partial view and is called using ajax call and after making an edit to the Building Offer record. I am expecting to get NO id but the url is appending an id somehow.

UPDATE:

I found the following question helpful to understand why I was facing this problem and also suggested work arounds: Prevent ambient route values from the URL from being added to Html.Action and Html.ActionLinks automatically in ASP.NET

Also, the following section in Microsoft Docs. discusses this issue: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-5.0#url-generation-reference

7
  • 1
    what happens if you omit the null parameter? Commented Apr 30, 2021 at 9:51
  • I get the same result as if null was present. (The id is still passed). Commented Apr 30, 2021 at 10:17
  • The weird thing is that it works properly the first time generated, but when I call that page using ajax call after editing a record, it passes the id of the record I just updated. Commented Apr 30, 2021 at 10:24
  • Why dont use just var x = " /BuildingOffers/AddOrEditSaleOffer" ??? Commented Apr 30, 2021 at 11:09
  • 1
    @Sergey using Url.Action is considered more robust. The route pattern might change and a literal string would then break, while Url.Action generates the correct url. Commented Apr 30, 2021 at 11:11

2 Answers 2

1

If you don't want to send parameters in your url, you can use the other Action method overload, which takes controller and action as input parameters.

var url = @Url.Action("AddOrEditSaleOffer", "BuildingOffers");
Sign up to request clarification or add additional context in comments.

1 Comment

I tried this but produced the same output. (It still passed the id of the previous edited record ).
1

you can use something like this

 string url = " /controllerName/ActionName

:)

3 Comments

Thanks for your suggestion. I think this might solve the issue but I want to know why my code does not work.
As pointed in the comments above: "The route pattern might change and a literal string would then break, while Url.Action generates the correct url."
in Action function that you use the description is: Generate URL with specific route value, maybe this is for it. please if the answer is useful, please rate.

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.