0

We have an issue whereby Outlook is misbehaving because it is attempting to read an autodiscover.xml on our domain, specifically https://example.com/autodiscover/autodiscover.xml.

The email host says that we should configure the websitefirewall to return a timeout error. However, the site is hosted on Azure, and I don't see any option to do that.

Therefore, I was wondering if this could be done in MVC? I tried the following:

public class AutoDiscoverController : Controller
{
    [Route("autodiscover/autodiscover.xml")]
    public ActionResult Index()
    {
        return new HttpStatusCodeResult(HttpStatusCode.RequestTimeout);
    }
}

...but it doesn't work; I see a 404 page instead. A breakpoint on the method also never gets hit.

Is it possible to create a route that mimics a non-existent file to return the timeout error?

Duplicate question update

There is a similar question at Dots in URL causes 404 with ASP.NET mvc and IIS. However, this question differs in that:

  • The answer doesn't work in MVC5
  • I cannot create a rule to recognise a trailing slash in the URL, because Outlook is generating the URL and I have no control over that

What else I've tried...

  1. In RouteConfig, I defined a custom route. It doesn't work; the route never gets hit.

    routes.MapRoute(
                name: "AutoDiscover",
                url: "autodiscover/autodiscover.xml",
                defaults: new { controller = "AutoDiscover", action = "Index" });
    
  2. As above, but using url: "autodiscover/*"

  3. routes.IgnoreRoute("autodiscover.xml");

  4. Defined a Route attribute on the controller itself, then enable routes.MapMvcAttributeRoutes();

EvilDr
  • 8,943
  • 14
  • 73
  • 133
  • Can you blacklist the IP of your exchange server? – allen.mn Sep 08 '17 at 16:01
  • 1
    Possible duplicate of [Dots in URL causes 404 with ASP.NET mvc and IIS](https://stackoverflow.com/questions/11728846/dots-in-url-causes-404-with-asp-net-mvc-and-iis) – NightOwl888 Sep 08 '17 at 16:50
  • @NightOwl888 no problem thanks. I worked through that answer and provided an update above – EvilDr Sep 11 '17 at 08:11
  • 1
    @EvilDr you are using attribute routing but I see no indication that you enabled it `routes.MapMvcAttributeRoutes();`. Making sure it is registered before convention-based routes. – Nkosi Sep 11 '17 at 08:25
  • @Nkosi Thanks. I added it in, but the route/controller still doesn't get hit – EvilDr Sep 11 '17 at 08:29

1 Answers1

2

I found an answer, thanks to Rick Strahl's answer here, and related blog post.

For some reason, defining a route in the RouteConfig never hits, but doing it at controller level does work. However, an additional step is needed, as pointed out by Nkosi to get it to work.

1. Enable MVC Attribute Routing

This wires up the routing attributes at controller-level (done in the next step)

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapMvcAttributeRoutes(); // <-- Added this

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

2. Add the Route as an attribute

public class AutoDiscoverController : Controller
{
    [Route("autodiscover/autodiscover.xml")]
    public ActionResult Index()
    {
        return new HttpStatusCodeResult(HttpStatusCode.RequestTimeout);
    }
}

3. Configure a route based on specific files in web.config

I believe this forces MVC to take over what IIS would usually handle (as XML is considered a "static" file).

<system.webServer>
  <handlers>
    <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
    <remove name="OPTIONSVerbHandler" />
    <remove name="TRACEVerbHandler" />
    <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    <add name="dotless" path="*.less" verb="GET" type="dotless.Core.LessCssHttpHandler,dotless.Core" resourceType="File" preCondition="" />

    <!-- Added this line -->
    <add name="AutoDiscoverXmlFileHandler" path="autodiscover.xml" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" responseBufferLimit="0" />

  </handlers>
</system.webServer>

Result

enter image description here

EvilDr
  • 8,943
  • 14
  • 73
  • 133