'Why does return RedirectToAction("/Index") work and RedirectToAction("Index)" not work

For a long time, I've had an action that returned

return RedirectToAction("Index","Vendor");

and it worked as expected. At the completion of this function, my Index function was called.

However, lately it stopped working. That same line of code now directs the browser to the url localhost:67676/Vendor/, my Index() action is never called and the page displays:

HTTP Error 403.14 - Forbidden

The Web server is configured to not list the contents of this directory.

However, if i add a forward slash to the method parameters like

return RedirectToAction("/Index","Vendor");

Everything works as expected and the Index function is called.

Any idea why I need to use "/Index" now but "Index" worked before



Solution 1:[1]

The error means that you have a folder in your app named Vendor and the url is trying to navigate to that folder rather than your VendorController. To solve the problem, rename the folder so that it does not match a controller name.

To understand what is happening behind the scenes,

return RedirectToAction("Index","Vendor"); internally looks at your route definitions for a match, and it matches your default route which has defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }. Because you pass "Vendor" as the controller, which does not match default, the first segment of the url becomes Vendor. And because your pass "Index" as the action, it matches the route, so no additional segment is added (its not required). The final url becomes localhost:67676/Vendor which matches your folder (hence the error).

When you used return RedirectToAction("/Index","Vendor");, your passing "/Index" which does not match the default action ("/Index" != "Index") so it generates a 2nd segment in the url which now becomes localhost:67676/Vendor/Index which will hit the controller because you don't (and could not) have a folder named "Vendor/Index"

Solution 2:[2]

It's a little late, but I'm posting it under this thread because I ran into the same problem. If there is a folder with the same name as Controller in your project file, you are encountering this problem. Example; VendorController - Vendor (Folder Name) I encountered this problem because of the wrong path I wrote when I wanted to create a new folder to save an image file. Be careful when creating "Server.MapPath".

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Huseyin Gokdemir