0

I have an ASP.NET MVC web project that has folders Controllers, Views, App_Data, App_Start etc. There is another folder called script at the top level which has a foo.js file.

When I run the website on IIS Express and access foo.js via my browser making a http request, I can see the contents of the file on the browser i.e when I do localhost:8000/scripts/foo.js. So I created another folder at the same level where this script folder is and named it react, and put a bar.js file into to.

But when I navigate to localhost:8000/react/bar.js, I get a HTTP Error 500.0 - Internal Server Error.

I've tried to search a lot what's going on, but couldn't make sense. Some say its a setting in web.config that restricts direct access to the file via IIS. Some say it is a configuration in IIS itself where certain folders are whitelisted for direct access other's are not Any help will be appreciated.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
mihsa
  • 21
  • 5
  • HTTP 500 error just means something went wrong on the server side. You'll need to figure out what the underlying error actually was. Only then can you begin troubleshooting the issue. You might look at the Application_Error event in your app, or disable the custom errors page, or check the Windows Event Viewer. – mason Mar 02 '23 at 19:21
  • "The controller for path '/react/bar.js' was not found or does not implement IController." is the error I got where as for '/scripts/foo.js' I get the actual file on the browse i.e, the application doesn't look for a controller in this case so I'm thinking it might be the configuration for these folders. @mason – mihsa Mar 02 '23 at 21:19
  • Do you have a controller called ReactController by any chance? Anyways, you should share your routing configuration as a [mcve]. – mason Mar 02 '23 at 23:18
  • No I don't have ReactController or a Scripts controller. – mihsa Mar 02 '23 at 23:38

1 Answers1

1

I was getting the error message "The controller for path '/react/bar.js' was not found or does not implement IController" when trying to access a file on an ASP.NET server, it's because the routing engine is looking for a controller to handle the request, but no controller is defined for the requested path.

In an ASP.NET MVC application, requests are mapped to controller actions based on the URL of the request. When you request a file on the server, such as "/react/bar.js", the routing engine looks for a controller that can handle the request based on the URL.

In this case, it seems like the routing engine is trying to find a controller that can handle the request for the "/react/bar.js" URL, but no such controller is defined in your application. Hence the error.

To resolve this issue, you can configure the routing engine to ignore requests for all files (or just certain files) under specific folder. This will allow requests for those files to be served directly by IIS without going through the MVC pipeline. To do this, you can add a route to the RouteConfig.cs file that specifies the file extension to ignore, like this:

routes.IgnoreRoute("react/{*pathInfo}");

mihsa
  • 21
  • 5
  • While you definitely posted a good answer, such has been discussed more than a decade ago when Microsoft initially introduced ASP.NET MVC framework. Do grab a good enough book to go through the entire technology so that you don't need to perform similar investigations just to find answers that are well known. – Lex Li Mar 04 '23 at 20:46