Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Team

What could be the reason why my routing is not working? I have configured this to my Routing.cs page. Please i need some assistance to this problem.

What I have tried:

// Route to CoursesRegistered
         routes.MapRoute(
             name:"CoursesRegistration",
             url:"courses-enrolled-for/",
             defaults: new {controller="Home", action = "CoursesRegistration", url = UrlParameter.Optional}

          );


[HttpPost]
 public ActionResult CoursesRegistration( eNtsaRegCourses model)
   {
       if(ModelState.IsValid)
       {
           try
           {
               cb.SaveChanges();
               return Json(new { success = true });
           }catch(Exception ex)
           {
               ModelState.AddModelError("", ex.Message);
           }
       }

       return PartialView("CoursesRegistration", model);
   }


   // saving changes here.
   static void SaveChanges(eNtsaCourses model)
   {

   }
Posted
Updated 20-Aug-20 1:54am

1 solution

Routes are resolved in the order you've added them so make sure your custom route is before the default route

routes.MapRoute(
    name: "CoursesRegistration",
    url: "courses-enrolled-for/",
    defaults: new { controller = "Home", action = "CoursesRegistration", url = UrlParameter.Optional }
);

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


If the default route is first it will take "courses-enrolled-for" as the controller name as the url matches the default pattern.
 
Share this answer
 
Comments
gcogco10 20-Aug-20 8:01am    
@F-ES Sitecore, my application is using AspNetIdentity, so below is my configuration from routing; // Route to CoursesRegistered
routes.MapRoute(
name: "CoursesRegistration",
url: "courses-enrolled-for/",
defaults: new { controller = "Home", action = "CoursesRegistration", url = UrlParameter.Optional }

);

// Route to home-page.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Index", id = UrlParameter.Optional }
); // This is used when used are registered/login on the site hope you get an idea
F-ES Sitecore 20-Aug-20 8:32am    
Well that config works for me, what is the url when you get a 404?
F-ES Sitecore 20-Aug-20 8:41am    
Reading between the lines, are you submitting the form which is requiring authentication so you are directed to the login form then back to your action? If so that is your problem as your action is HttpPost and the redirect is an HttpGet. When you redirect to your url after login it doesn't preserve the original form data so you'll need an httpget version of the action that redirects to the original form. The user will have lost their data though so ensure the person is logged in when they access the form, not after they have submitted it.
gcogco10 20-Aug-20 8:43am    
In another words [HttpGet] public ActionResult(....) {}?
gcogco10 20-Aug-20 8:50am    
I have tried its work, the issue i get now is not saving to my table from the database

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900