0

I have an MVC site running in a sub application within IIS which performs an Ajax request for additional content after the initial load.

The problem is that if a user fails to enter the trailing "/" at the end of the URL, the AJAX request fails because a "/" is added to the front of the requested URL by the XHR object. This results in it going to the root domain and leaving the application area.

Below is a Fiddler trace showing what I mean.

Fiddler Trace

The first 3 entries, show a basic HTML site which I set up to see what happens outside the MVC environment. In this case, when the trailing "/" is not provided, IIS sends a redirect to the URL with the trailing slash. The subsequent AJAX request then succeeds.

Within MVC in the next 2 entries, the redirect is not issued and the AJAX request then fails. Is there a way to get a 301 redirect (preferably from IIS) in the MVC scenario if the trailing "/" is missing?

1 Answer 1

2

You can configure the IIS URL Rewrite module to do what you need. See the first item on this page (first search result I came by).

Another option I would suggest is that you can generate your ajax request using something like @Url.Action("action", "controller") and I think that should take care of the slash issue for you. If you do this it shouldn't matter how your app is hosted, it should just work.

A fuller example of what I mean, using jQuery:

$.ajax({
    type: "POST",
    url: @Url.Action("action", "controller"),
    data: $("form").serialize(),
    success: function (result) {
        ...
    },
    failure: function () {
        ...
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I will give the URL rewrite a go and see if that works. I don't think I can use Url.Action in this case because the code is in external JS files.
URL rewrite doesn't seem to work, because it doesn't rewrite the root URL for the application. In the configuration for the URL rewrite module it's actually saying that it's matching after /ClientPortal/ so if the user provides /ClientPortal it remains the same.

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.