简体   繁体   中英

ASP.NET MVC5 - Sequential ID routing issue: 404 error when ID over 1 character long?

I'm having a little problem I assume is with my routing configuration. Below is my situation:

Situation

I have a page that list employees that are attached to an entity. From the entity page they can add new employees. Once an employee has been added they can then be clicked on to get in-depth information on the employee.

The creation and further information works fine, the employees are created and their ID increases as normal.

Problem

When an employee is created and assigned an ID over 1 character (which either happens at over 10 employees or when ASP.NET decides to jump ID about a thousand or so) the details view on the specific employee with 404 and simply display:

"The resource you are looking for has been removed, had its name changed, or is temporarily unavailable."

IDs in the range of 0-9 seem to work without any problem but as soon the ID goes over 1 character (to say, 10) the issue will occur.

RouteConfig.cs

         routes.MapRoute(
             "EmployeeDetails",
             "{controller}/{id}-{Title}/{action}/{eid}",
             new { controller = "Farm", action = "EmployeeDetails", id = UrlParameter.Optional, title = UrlParameter.Optional, eid = UrlParameter.Optional},
             new { id = @"\d+", eid = @"\d" }
         );

FarmsController.cs

        public ActionResult Employee(int id = -1, int eid = -1)
        {

        if(id == -1 || eid == -1){
            RedirectToAction("Index","Home");
        }
        Farm f = db.Farms.Find(id);
        ViewBag.FarmName = f.Name;
        ViewBag.FarmUrl = f.getLink(true);
        Worker w = db.Workers.Find(eid);
        w.TimeSheets = w.TimeSheets.OrderByDescending(m => m.TimeSheetID).ToList();

        return View(w);
    }

Thanks in advanced for any help, apologies if I haven't provided the all relevant information. First time poster please let me know if you need to know anything else.

Cheers

Your constraints should be like this:

new { id = @"\d+", eid = @"\d+" }

Currently you are using regular expression \\d for eid field, which means only single symbol

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM