Tomas
Tomas

Reputation: 18097

Create URL for action with attribute routing

I have action in controller with ASP.NET MVC 5 attribute routing.

public class HomeController : BaseController
{
  [Route("{srcFileFormat}-to-{dstFileFormat}")]
  public ActionResult Converter(string srcFileFormat, string dstFileFormat)
  {
  }
}

I am trying to create url and always get Null instead of URL. Any suggestion how to use UrlHelper.Action and Attribute Routing together in my case?

@Url.Action("docx-to-pdf", "Home")

Upvotes: 2

Views: 1149

Answers (1)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

In Url.Action we specify the controller name and action method name, not the routing naming or parameters the way you are trying.

We normally do the following way for getting url for a controller action:

@Url.Action("Converter", "Home")

But as your action method expects two parameters too and you are trying to pass those in, you will need to call it passing the parameters too, like:

@Url.Action("Converter", "Home", new {srcFileFormat ="doc",dstFileFormat="pdf"})

Now it should generate the url like:

localhost:6087/doc-to-pdf

Upvotes: 3

Related Questions