Dots in URL causes 404 with ASP.NET mvc and IIS

Cover Image for Dots in URL causes 404 with ASP.NET mvc and IIS
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

🌟🆘 Hey there, tech enthusiasts! Having trouble with those pesky dots in your URL causing a 404 error? Don't worry, we've got your back! In this blog post, we'll address the common issues related to dots in your URL with ASP.NET MVC and IIS and provide you with some easy solutions. Let's dive right in! 🚀

✅ The Problem: So, you have a project that requires URLs with dots in the path, but every time you try accessing a URL with a dot, you're greeted with an annoying 404 error. You've already checked your routing configuration, and that seems to be fine. You even tried adding a request filtering configuration to your web.config but, unfortunately, it didn't help. What's going on? 🤔

💡 The Solution: Fear not, dear reader! The issue you're facing is related to the dot being treated as a file extension by IIS, causing it to look for a physical file instead of matching the route in your MVC application. Luckily, there are a couple of straightforward solutions to get those dots working for you. Let's take a look at them! 💪

1️⃣ Solution 1: Disabling Static File Handling By default, IIS intercepts requests for static files such as images, CSS, and JavaScript. This can interfere with your MVC routing. To overcome this, you can disable static file handling for the specific URL pattern containing dots. Here's how you can do it:

<configuration>
  <system.webServer>
    <handlers>
      <remove name="BlockViewHandler" />
      <add name="BlockViewHandler" path="*.mvc" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
    <staticContent>
      <remove fileExtension=".phelps" />
      <mimeMap fileExtension=".phelps" mimeType="text/html" />
    </staticContent>
  </system.webServer>
</configuration>

In the above example, we're removing the static content handling for the .phelps file extension, which is just an example. Replace .phelps with your desired file extension containing the dot.

2️⃣ Solution 2: Allowing Dots in Routes Another option is to configure the route handler to allow dots in the URL. By default, ASP.NET MVC restricts the dot character for security concerns. To enable dots, update your route configuration like this:

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

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        constraints: new { id = @"\d+" }
    ).RouteHandler = new DotFriendlyRouteHandler();
}

The DotFriendlyRouteHandler class is responsible for allowing dots in your URL routes. You can find a sample implementation of this class in an accompanying code snippet here.

✅ The Call-To-Action: Congratulations, you've now learned how to tackle the 404 error caused by dots in your URL with ASP.NET MVC and IIS! Give these solutions a try in your project and wave goodbye to those frustrating errors. If you found this blog post helpful, be sure to share it with your fellow developer friends! 👥😄

Got any other burning tech questions or topics you'd like us to cover? Shoot them our way in the comments section below! We're always here to assist you in your coding adventures. Happy coding! 💻🔥


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello