简体   繁体   中英

Issue with Url routing - Asp.Net MVC Razor

I use "/Controller/ActionName" url format for my ajax calls. This pattern works fine with my development server that has this pattern

/Controller/ActionName

My application has been deployed to IIS with the following url pattern

/domain/Controller/ActionName

So whenever I call a resource it routes to /Controller/ActionName instead of /domain/Controller/ActionName there by resulting in 404 error. Is there any quick fix for this by modifying the routeconfiguration such that it works on both the devl as well as the IIS.

If you defining the relative urls "manually" as a string try not using a '/' in the beginning.

"Controller/ActionName"

Edit

What I've done the the past is included a Partial Razor View that is responsible of setting urls using @Url.Action()

Partial - Shared/JavascriptUrlsPartial.cshtml

<script type="text/javascript">
  (function ($, app) {

    app.urls =
    {
      actionA: '@Url.Action("Controller", "ActionA")',
    };

  })(jQuery, app = app || {});

</script>

Then you reference the rendered url in javascript.

$.ajax({
  url: app.urls.actionA,
  //etc
})

This partial is then included in the _Layout.cshtml.

In my Razor view, we have Ajax calls like:

        var model = { searchId: searchId, newSearchName: searchName, newSearchDescription: description };
        $.ajax({
            url: '@Url.Action("Rename", "Search")',
            contentType: 'application/json; charset=utf-8',
            type: 'POST',
            dataType: 'html',
            data: JSON.stringify(model)
        })

We have not done anything special in local or production environment. We host the application on IIS 7.5.

I could not make out what's different in your case from your question and comments. If this does not solve the problem please add more information around your setup.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM