malshep malshep - 10 months ago 11040
TypeScript Question

ASP.Net Core 2 - Using Url.Action to generate url to web api behaves differently?

I've used the helper Url.Action to pass relative URLs from MVC views to Typescript/Javascript classes before in full ASP.Net MVC with success. Here is an example, in a cshtml file:

<script>
let vm = new VM('@Url.Action("Names", "api/Data")');
</script>


The Typescript class constructor is passed the following string in full ASP.Net: "/api/Data/Names", but in ASP.Net Core 2, the passed URL is "/api%2FData/Names", which is an invalid URL. I can't figure out if this is intended or a bug.

Thoughts? Thanks.

Answer Source

Url.Action(action, controller)

Specify the controller name, not the route, e.g...

Url.Action("names", "data")

And then on the controller itself you can do something like...

[Route("api/data")]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download