Donald Jansen
Donald Jansen

Reputation: 1985

MVC ActionLink GET not working

I have the following link

<a href="@Url.Action("Index", "Inspection")"><i class="fa fa-user-md"></i> Inspections</a>

In my route I have the following

routes.MapRoute("Inspections", "{controller}/{action}/{id}", new { controller = "Inspection", action = "Index", id = UrlParameter.Optional });

In Visual Studio when I click the link it works, on my live server I get a white page with the following in bold xxx - /inspection/ and also a link [To Parent Directory]

All of my other links in the Application is working fine and as expected Now I have to go and overide the url in the address bar to go xxx/Inspection/Index

The Index method does also have [HttpGet]

Edit 1: I tried the following, I also tried removing the route

routes.MapRoute("Inspection", "{controller}/{action}/{id}", new { controller = "Inspection", action = "Index", id = UrlParameter.Optional });

Edit 2:

Here is all the routes I have

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute("Login", "{controller}/{action}/{id}", new { controller = "Login", action = "Index", id = UrlParameter.Optional });
        routes.MapRoute("Employees", "{controller}/{action}/{id}", new { controller = "Employees", action = "Index", id = UrlParameter.Optional });
        //Added the following to try to fix issue
        routes.MapRoute("Inspection", "{controller}/{action}/{id}", new { controller = "Inspection", action = "Index", id = UrlParameter.Optional });

Edit 3: in InspectionController.cs I have the following Index method

    // GET: Inspection
    [HttpGet]
    public ActionResult Index()
    {
        return View(new ActiveInspectionsViewModel());
    }

Edit 4:

Here is the blank page that opens (example)

xxx - /Inspection/

xxx - /Inspection/


[To Parent Directory]


Edit 5:

I think were getting closer to the issue, I have disabled Directory Browsing now I am getting the 403 error, which is really weird because all my other Action links works (/Employees, /InspectionTemplates, /Clients, /Emails, /CompanyLevels) It is Just /Inspection that is causing the problem, my url is inspection.xxx.co.za can it be the park domain causing the problem?

Upvotes: 0

Views: 954

Answers (1)

Shyju
Shyju

Reputation: 218702

Look at your route definitions.

routes.MapRoute("Login", "{controller}/{action}/{id}", 
   new { controller = "Login", action = "Index", id = UrlParameter.Optional });

routes.MapRoute("Employees", "{controller}/{action}/{id}",
          new { controller = "Employees", action = "Index", id = UrlParameter.Optional });

routes.MapRoute("Inspection", "{controller}/{action}/{id}",
          new { controller = "Inspection", action = "Index", id = UrlParameter.Optional });

All three definitions you registered has the same url pattern, {controller}/{action}/{id}.

So whenever you request a page, The request url will be matched against the registered route definitions and when it gets the first match, the request will be send to the controller-action specified in the definition.

So when you request Inspection/Index, it is matching to the first definition (Login) and the request will be send to Login/Index action method.

Since you do not have anything other than the custom pattern in all the three routes,you do not need those. Just remove those and keep the default one.

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 }     
    );
}

Upvotes: 1

Related Questions