0

I have following methods in my MVC controller:

[HttpGet] public ActionResult Add(Guid b) { ViewBag.Title="Add Location"; //init some info return View("Edit",<Model>); } [HttpGet] public ActionResult Edit(Guid l) { ViewBag.Title="Edit Location"; //get object from db return View("Edit",<Model>); } 

Following is the route registration:

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

Now when I try to access following two routes:

1. http://localhost:60732/Location/Add?b=512f770f-51c3-4791-8eba-61fe753e2a83 2. http://localhost:60732/Location/Edit?l=512f770f-51c3-4791-8eba-61fe753e2a83 

First one works but 2nd gives 404.

I have also tried AttributeRouting with following routes:

[HttpGet] [Route("Location/AddLocation/{g}")] public ActionResult Add(Guid g) [HttpGet] [Route("Location/EditLocation/{l}")] public ActionResult Edit(Guid l) 

Again,

 http://localhost:60732/Location/AddLocation/512f770f-51c3-4791-8eba-61fe753e2a83 

works but

 http://localhost:60732/Location/EditLocation/512f770f-51c3-4791-8eba-61fe753e2a83 

does not.

What am I doing wrong?

Strangely, if I pass wrong Guid like:

 http://localhost:60732/Location/EditLocation/512f770f 

It gives following error:

The parameters dictionary contains a null entry for parameter 'l' of non-nullable type 'System.Guid' for method 'System.Web.Mvc.ActionResult Edit(System.Guid)' in 'AppointmentScheduler.Controllers.LocationController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters 
4
  • I see nothing wrong with this code, it should work. Are we missing something here? Commented Dec 29, 2014 at 2:26
  • @DavidG that's what I am not able to figure out. There is nothing fancy there but still it is not working. Commented Dec 29, 2014 at 2:27
  • 1
    With your edit, it seems the error is originating inside the action method. Does that throw a 404 error or an HttpNotFound exception? Commented Dec 29, 2014 at 2:30
  • 1
    Yes @DavidG it was inside mthod and returned HttpNotFound Commented Dec 29, 2014 at 2:32

2 Answers 2

2

There is nothing wrong with your routing here. However, the code in your Edit method must itself be throwing a 404 error by throwing HttpNotFound.

Sign up to request clarification or add additional context in comments.

Comments

1

I just hope this is last WTF of 2014. I pass the id (Guid) and try to fetch object from db. Actually, it is not able to find it and I'm returning: HttpNotFound (I think copied from some API method). So I am always getting 404 for this route.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.