1

There are many older examples with this kind of syntax (from the question):

Here's a typical example of what you should never do:

<script type="text/javascript">
    $.ajax({
        url: '/home/index'
    });
</script>

and here's how this should be done:

<script type="text/javascript">
    $.ajax({
        url: '@Url.Action("index", "home")'
    });
</script>

Running an ASP.NET MVC app from a virtual directory in IIS7

I then found UrlHelper configuration is more difficult than in previous versions of MVC,

Unable to utilize UrlHelper

Are the Url Action methods "gone" or not what should be used now? Does Core want URL rewriting? How do I create a dynamic url string in Core 1.1 for JavaScript the "Core" way? I could build it manually and pass it to my View, but that is not what I wanted to do.

(no jQuery please, and yes I know the question above has jQuery)

Edit: In my JavaScript I have,

var xhr;
xhr = new XMLHttpRequest();
var url = //this is what I want Asp.Net to generate.  Currently I manually build it within the JavaScript.  It should be,

var url = 'Home/Index' //this can change possibly depending on where the application is deployed.

I have tried this:

 @UrlHelper url = new UrlHelper(HttpContext.Current.Request.RequestContext)
var url = '@url.Action("index", "home")';

It does at the first Razor code with:

"The name 'UrlHelper' does not exist in the current context"

I can probably figure this part out (I have tried it with the full namespace as well). My main question is if this is still the right way to do things in Core. It seemed it might not be so which is when I stopped and asked here. I thought I was missing something.

Community
  • 1
  • 1
johnny
  • 19,272
  • 52
  • 157
  • 259
  • In asp.net core, you can use `Url.Action` helper method in your views. What happens when you try that ? – Shyju Mar 22 '17 at 15:22
  • @Shyju I can try it if I do more configuation, but when I saw what needed to be done, it did not seem that MS wanted the Url.Action method to be used. I also believe I might be confusing the Url Helper methods with the HTML ActionLink type helpers and/or Tag Helpers. – johnny Mar 22 '17 at 15:25
  • There is no configuration needed to use the helper methods. – Shyju Mar 22 '17 at 15:26
  • The name 'UrlHelper' does not exist in the current context, which is when I started reading about the configuration and started to get confused. – johnny Mar 22 '17 at 15:27
  • The code you shared does not have anything like `UrlHelper` ! You are not showing some code which is causing the error – Shyju Mar 22 '17 at 15:36
  • What happens if you change `@url.Action...` to `@Url.Action...`, and remove the previous line – Brian Mar 22 '17 at 15:50
  • @Brian It worked. Thank you. Is using the Url.Action method(s) the current "Core" way to do it. – johnny Mar 22 '17 at 16:31

0 Answers0