0

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.

6
  • If you use the Url helper as in Url.Action("Action", "Controller") it should take care of generating the right paths for you. Commented Oct 20, 2014 at 7:56
  • @Candide if I use the above , my url is rendered as /@Url.Action(%22action%22,%22controller%22) Commented Oct 20, 2014 at 7:58
  • because it is not inside my view . I need to use the relative path since all my js files are external Commented Oct 20, 2014 at 8:03
  • Using relative paths is a bad solution. Once you refactor code, it becomes difficult to ensure all paths point correctly. Use a global javascript variable to store what ~ points to, and rewrite the path in javascript code. Something like "~/Controller/Action".replace("~", globalPath) Commented Oct 20, 2014 at 9:12
  • Is there a way to make my root path like localhost/DomainName Commented Oct 20, 2014 at 10:37

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

unfortunately it works only for localhost/controller urls .. Not for localhost/controller/action urls. It is rendered as localhost/controllerA/ControllerB/actionA
Edited with a alternative approach
0

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.

1 Comment

Hi thanks for the response. As I have mentioned all my js are external hence I cannot use @Url.Action

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.