Redirection from dir name does not work

I have a ASP.NET Web Site using ServiceStack, The site has index.html page which is supposed to be the default page.

When I run it under IIS Express and the application is at the site root, it works OK.
However when I run it under IIS and provide virtual dir name without trailing slash, the application does not work since Web Server returns directly page content instead of HTTP 302 with redirection to URL with / at the end and relative paths in the page resolved incorrectly (on the web server root level instead of app virt dir).

This works fine without ServiceStack with pure ASP.NET
With SS 4.038 I managed to bypass this using DefaultRedirectPath = “/index.html”, however after upgrade to 4.0.50 it does not work anymore (I also installed ServiceStack.Razor, but I do not think it is relevant).

Regards, Leonid

So you want to redirect to the page and not tread index.html as a default document?

Try removing it as a Default Document:

SetConfig(new HostConfig {
    DefaultDocuments = new List<string>()
});

Otherwise try renaming it to a non default document like home.html.

I want to use it as default document, but it should not be returned for request http://sever/dir
Such request should return HTTP 302 with redirection URL http://sever/dir/ and that URL should return the document.

What happens now is that index.html contains <script src="somescript.js"> and browser tries to retrieve the file through URL http://server/somescript.js instead of http://server/dir/somescript.js

This is host-specific where some ASP.NET hosts will redirect directories (i.e. before it reaches ServiceStack).

You can have ServiceStack do this in your AppHost with:

RawHttpHandlers.Add(RedirectDirectory);

Which registers a custom http handler that will check every request to ensure requests to directories have a / suffix otherwise will automatically redirect.

Although I’d recommend that you always link to the url with the / suffix and save the latency of a redirect, e.g: http://server/dir/

How I can ensure / at the end of URL? User enters it, so it is out of my control.

I’ll try your suggestion with the handler, but it looks like as a workaround. I suggest ServiveStack will handle this correctly.

Thanks, Leonid

As I said earlier it’s not ServiceStack that handles auto directory redirects for ASP.NET websites, its specific to the ASP.NET host.

The code I’ve provided is how you tell ServiceStack it should add the overhead of checking every request. But if the ASP.NET host already does this you don’t want to add the overhead of checking every request again - which is why it’s opt-in.

Unfortunately
RawHttpHandlers.Add(RedirectDirectory);

did not help.

Maybe

would solve this issue, but here I have another problem:

I tried to add ServiceStack.Razor and renamed my page index.html to index.cshtml. Now it works under IISExpress in root dir, but fails under non-root Web directory on redirecting the URL to http://localhost/index. I read that this is “Pretty urls by default” feature of Razor, but how I get rid of it? This redirected naturally fails with 404, so the page is not dispalyed.

I meant:

I’m trying to paste some line but the forum cuts the beginning. Before @Url there was lt; base href="

Going to need to see a repro of this to understand and identify what the exact issue is. Can you provide a small standalone project that has the issue (e.g. on GitHub).

FYI: If you want to post code or xml in Markdown you need to escape it, either with triple backticks, e.g:

```
You can escape code or xml here
```

Or by just indenting the code or xml 4 spaces in between new lines, e.g:

<this><is><xml>after 4 spaces</xml>

I understood that the problem was caused by:

<add key="webPages:Enabled" value="false" />

in web.config