1

I am developing a dashboard with rendering different KPI's. There is also an interaction with a tool, that's why I have chosen MVC.

Now I have a layout page with a navigation bar. In the Navigation bar I am linking with @Url.Action. So far so good. Every page is working, and if a link is done where exists an other Controller, it works, too. Only in one link I have a problem - I get always the "resource not found" errorpage.

The page LockedOrder is not working.

My layout view looks like this:

<ul class="treeview-menu">
                        <li>
                            <a href="@Url.Action("Index", "Home")"><i class="fa fa-circle-o text-yellow"></i>Lab Dashboard<i class="fa fa-angle-left pull-right"></i></a>
                            <ul class="treeview-menu">
                                <li><a href="@Url.Action("Index", "Home")"><i class="fa fa-circle-o"></i>Home</a></li>
                                <li><a href="@Url.Action("Load" , "LockedOrder")"><i class="fa fa-circle-o"></i>Locked Order</a></li>
                            </ul>
                        </li>
                        <li><a href="@Url.Action("Index","DashboardXY" )"><i class="fa fa-circle-o text-blue"></i>DashboardXY</a></li>
                    </ul>

Here is my LockedOrder Controller:

public class LockedOrderController : Controller
{
    List<string> table = new List<string>();

    // GET: LockedOrder
    public ActionResult Load()
    {
        table = LoadTable(table);
        return View();
    }
}

A view Load.cshtml exists on the correct view folder.

My RouteConfig file:

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

        // Locked Order
        routes.MapRoute(
            name: "LockedOrder",
            url: "LockedOrder/{Load}/{id}",
            defaults: new { controller = "LockedOrder", action = "Load" }
        );

        // Dashboard Resources Department Route
        routes.MapRoute(
            name: "DashboardXY",
            url: "dashboard/{ departmentchooser}",
            defaults: new { controller = "DashboardXY", action = "depchooser" }
        );

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

So why are all links working and only the link of LockedOrder not?

Please help me. I'm really new in ASP.Net MVC, so if something is not clear, just ask me :)

Thanks!

Update: This is my errormessage which I get (I'm sorry, its in German, but may be it can help you to help me :) )

Update 2 Now my code looks like this: RouteConfig LockedOrderController

Azeristar
  • 127
  • 2
  • 3
  • 11
  • Do you need the `id` parameter in the route? `LockedOrder/{Load}/{id}`? Or even the `Load` parameter? – DavidG Jun 02 '16 at 10:30
  • `url: "LockedOrder/{action}",` or `url: "LockedOrder/Load",` –  Jun 02 '16 at 10:30
  • @DavidG no, you're right. I removed it. But it still does not work. – Azeristar Jun 02 '16 at 10:31
  • @StephenMuecke thanks for your help, but this is still not working for me. I get still the Resource can not be found error message – Azeristar Jun 02 '16 at 10:33
  • What is the point of have 3 route definitions? All you need is the default route with `url: "{controller}/{action}/{id}",` (with `id = UrlParameter.Optional`) –  Jun 02 '16 at 10:33
  • @StephenMuecke I have seen in MVC Tutorials that they are adding a route, so that it is knows which Controller is going to be taken – Azeristar Jun 02 '16 at 10:41
  • There no need for this at all - they are matched by the standard 'Default' route. If for example you wanted to have the route as `../order/load` instead of `../lockedorder/load` then that would be a reason for a specific route. –  Jun 02 '16 at 10:45
  • @StephenMuecke oh okay. I removed it now, just to try, but it is still not working.. Again the same error message – Azeristar Jun 02 '16 at 10:47
  • Then all I can suggest is you have a typo somewhere. –  Jun 02 '16 at 10:50
  • If you still have not resolved this yet, the I suggest you debug your routes using [routedebugger](http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx/) –  Jun 03 '16 at 00:04
  • @StephenMuecke yes it is still not resolved. Thank you for the hint. I will try the routedebugger – Azeristar Jun 03 '16 at 08:52

2 Answers2

1

try to change this route config:

// Locked Order
    routes.MapRoute(
        name: "LockedOrder",
        url: "LockedOrder/{Load}/{id}",
        defaults: new { controller = "LockedOrder", action = "Load" }
    );

to this:

// Locked Order
    routes.MapRoute(
        name: "LockedOrder",
        url: "LockedOrder/{action}/{id}",
        defaults: new { controller = "LockedOrder", action = "Load", id = UrlParameter.Optional }
    );
mustint
  • 49
  • 1
  • 11
  • Thanks. I have tried this, but it didn't helped. I get still the "resource can not be found" error message – Azeristar Jun 02 '16 at 10:43
  • @azeristar can you give a link for this page online . and can you give us a photo of error page – mustint Jun 02 '16 at 10:50
  • I have uploaded a picture in the Update part. Unfortunately I can not provide a link, because it is an Intranet Webinterface at the company where I am working – Azeristar Jun 02 '16 at 10:54
  • sorry for this, but can you give a photos of your routes code and your menu .cshtml and LockedOrderController.cs ... after you did previous changes. – mustint Jun 02 '16 at 11:07
  • I have uploaded it in Update 2 :) – Azeristar Jun 02 '16 at 11:19
1

The following Url.Action doesn't work

@Url.Action("Load" , "LockedOrder")

because you specified id as a required parameter in the LockedOrder route.

    routes.MapRoute(
        name: "LockedOrder",
        url: "LockedOrder/{Load}/{id}", // {id} is required because there is no default.
        defaults: new { controller = "LockedOrder", action = "Load" }
    );

Option 1: Remove the {id}

    routes.MapRoute(
        name: "LockedOrder",
        url: "LockedOrder/{Load}",
        defaults: new { controller = "LockedOrder", action = "Load" }
    );

Option 2: Mark {id} as Optional

Note this will have the effect of making both /LockedOrder/Load/123 and /LockedOrder/Load as active URLs going to the same action method.

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

Option 3: Supply the id as a Route Value in Url.Action

@Url.Action("Load", "LockedOrder", new { id = 123 })
NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • Thanks. I have removed the ID path, but it still doesn't work. I get again the Error message "Resource can not be found". – Azeristar Jun 02 '16 at 10:45
  • I am guessing you left the last `/` in the `url` string. See my edit. – NightOwl888 Jun 02 '16 at 10:58
  • Thanks, No I have checked it, I have realized Option 2 and I just have copied your solution, so that there are no chances for typing mistakes. – Azeristar Jun 02 '16 at 11:01
  • Nothing you have posted here would explain why it doesn't work. Are you using Areas or attribute routing? If so, pleas post the relevant parts of your configuration. – NightOwl888 Jun 02 '16 at 11:25
  • FYI - there is absolutely no reason you need a custom route in this case. If you revert the `DefaultRoute` back to its original settings of `url: "{controller}/{action}/{id}"` and add the default `id = UrlParameter.Optional`, it will cover this case fine (and you should remove the `LockedOrder` route entirely from `RouteConfig`). – NightOwl888 Jun 02 '16 at 11:25
  • I have removed it once, just to try and it was still not working. But just to know, when do I need an additional RouteMap? I have seen in MVC tutorials, that they are setting defaults with controllers for different pages. That's why I have built them in. – Azeristar Jun 02 '16 at 11:31
  • You need a custom route map when the default 3-segment URL won't cover your case. Sometimes you need a 5-segment URL, or some like to ensure any URL that does not match a real controller throws a 404, for example. Microsoft succeeded in making routing simple to configure and powerful, but they failed in making it intuitive to understand. The main thing to keep in mind is that the route table is run from the first registered route to the last and the first match always wins. See [this post](http://stackoverflow.com/a/35674633/181087) for some insight. – NightOwl888 Jun 02 '16 at 11:42
  • I have reinstalled my Visual Studio because of some other issues and now it works. – Azeristar Jun 03 '16 at 14:37