Using MVC 3 Razor, how can I specify a variable within a call to @Url.Content()
.
Example:
@{
var myVar = Request.QueryString["foo"];
}
<a href="@Url.Content("~/bar?@myvar")">click here</a>
To declare a variable in the View using Razor syntax, we need to first create a code block by using @{ and } and then we can use the same syntax we use in the C#. In the above code, notice that we have created the Code block and then start writing C# syntax to declare and assign the variables.
You can add support for Pages to any ASP.NET Core MVC app by simply adding a Pages folder and adding Razor Pages files to this folder. Razor Pages use the folder structure as a convention for routing requests.
Url.Content is used when you wish to resolve a URL for any file or resource on your site and you would pass it the relative path: @Url.Content("~/path/file.htm") Url.Action is used to resolve an action from a controller such as: @Url.Action("ActionName", "ControllerName", new { variable = value })
Multi-statement Code blockYou can write multiple lines of server-side code enclosed in braces @{ ... } . Each line must ends with a semicolon the same as C#.
You're already in the c# context at that point, so you can use the variable just like you would in a code file. Try this:
<a href="@Url.Content("~/bar?" + myvar)">click here</a>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With