weixin_33704234 2019-06-10 15:18 采纳率: 0%
浏览 52

C#Mvc Ajax baseUrl

So,

I have an application that uses MVC Areas. The route for the controller looks like

[Route("Settings/{something}")]

Then I have an Action

  [Route("suggest/{term}")]
  public ActionResult Suggest(string something, string term).

I try to call the Suggest action from javascript

var suggestUri = "suggest/";
$searchInput.autocomplete({
    source: function (request, response) {
        $.ajax({
            type: "GET",
            url: suggestUri + request.term,
            dataType: "json",
            success: function (data) {
                response(data);
            },
            error: function (err) {
                console.log(err);
            }
        });
    },
    minLength: 3
});

From my point of view this ajax call url should be

 http://localhost:7311/Settings/firstValue/suggest/term

But the actual result is

 http://localhost:7311/Settings/suggest/term

It's missing the {something}

There is for sure something I do wrong, but I have no idea what.

LE: I've done some tests.

If I apply to my controller a different route like

[Route("Settings/{something}/a")]

The result is

 http://localhost:7311/Settings/firstValue/suggest/term

When it should be

http://localhost:7311/Settings/firstValue/a/suggest/term

Then i tried to do

[Route("Settings/{something}/a/b")]

The result is

 http://localhost:7311/Settings/firstValue/a/suggest/term

When it should be

http://localhost:7311/Settings/firstValue/a/b/suggest/term

It feels like the mvc always leaves out the last parameter from my controller. Any reason?

LE2 :

In Startup.cs the MapRoute looks like this

  app.UseMvc(routes =>
            {
                routes.MapRoute(
               name: "somethingBased",
               template: "{area:exists}/{somethingBased?}/{controller=Home}/{action=Index}");

                routes.MapRoute(
                    name: "home",
                    template: "{controller=Home}/{action=Index}/{application?}");

            });
  • 写回答

1条回答 默认 最新

  • elliott.david 2019-06-11 00:11
    关注

    Assuming you are rendering your javascript inside a Razor view, you can always use @Url.Action to calculate the URL rather than try to calculate it from javascript.

    $searchInput.autocomplete({
        source: function (request, response) {
            $.ajax({
                type: "GET",
                url: @Json.Serialize(Url.Action("Suggest", new { term = '$TERM$' })).replace('$TERM$', request.term),
                dataType: "json",
                success: function (data) {
                    response(data);
                },
                error: function (err) {
                    console.log(err);
                }
            });
        },
        minLength: 3
    });
    

    This is using Url.Action("Suggest", new { term = '$TERM$' }) to calculate a template to your MVC's routing server side, which returns Settings/firstValue/suggest/$TERM$. Then @Json.Serialize converts that to javascript literal "Settings/firstValue/suggest/$TERM$".

    In javascript, you string.replace $TERM$ with your dynamic value. The javascript rendered by razor will look like this:

    url: "Settings/firstValue/suggest/$TERM$".replace("$TERM$", request.term)

    The advantage of this approach is that if you change your routing in MVC later, the client side code still works.

    If you need to specify controller, area, or other route values that may not be inferred from the current context, Url.Action has plenty of overloads to handle that.

    评论

报告相同问题?