Home > Mobile >  Generate a url link in .net core api with route values
Generate a url link in .net core api with route values

Time:10-14

[route("{value}/download")]
Public IactionResult Download([fromroute] string value)
{
}

I want to generate a link for the above endpoint, I have tried using below actionlink to generate the URL but it's returning a null value.

Output shoud be https://{hostname}/{controlername}/{routevalue}/actionname

Url.ActionLink($"DownLoad", "controller", new { Value= value}, protocol: "https")
Url.ActionLink($"{value}/DownLoad", "controller", new { Value= value}, protocol: "https")

CodePudding user response:

I have tried using below actionlink to generate the URL but it's returning a null value

That is because UrlHelper cannot find such action name({value}/DownLoad) in controller.

You need check the Url.ActionLink source code to learn what each parameter represents:

//
// Summary:
//     Generates an absolute URL for an action method, which contains the specified
//     action name, controller name, route values, protocol to use, host name, and fragment.
//     Generates an absolute URL if the protocol and host are non-null. See the remarks
//     section for important security information.
//
// Parameters:
//   helper:
//     The Microsoft.AspNetCore.Mvc.IUrlHelper.
//
//   action:
//     The name of the action method. When null, defaults to the current executing action.
//
//   controller:
//     The name of the controller. When null, defaults to the current executing controller.
//
//   values:
//     An object that contains route values.
//
//   protocol:
//     The protocol for the URL, such as "http" or "https".
//
//   host:
//     The host name for the URL.
//
//   fragment:
//     The fragment for the URL.
//
// Returns:
//     The generated URL.
//
// Remarks:
//     The value of host should be a trusted value. Relying on the value of the current
//     request can allow untrusted input to influence the resulting URI unless the Host
//     header has been validated. See the deployment documentation for instructions
//     on how to properly validate the Host header in your deployment environment.
public static string ActionLink(this IUrlHelper helper, string action = null, string controller = null, object values = null, string protocol = null, string host = null, string fragment = null);

Then you need change the code like below:

@Url.ActionLink($"Download", "ControllerName", new { Value= value}, protocol: "https")

Backend code:

[Route("[controller]")]
public class HomeController : Controller   //controller name here is Home
{
    [Route("{value}/download")]
    public IActionResult Download([FromRoute] string value)
    {
        return Ok();
    }
}

CodePudding user response:

This code was tested in visual studio

if you need a link, for your complicated route you have to use a route name

[Route("[controller]/{value}/download", Name ="download")]
public IActionResult Download(string value)

and view

@Html.RouteLink( "Go to download", "download", "https",null,null, new { Value=value},null)

it will generate url

http://xxxx/controller/value/download
  • Related