ASPCoder1450
ASPCoder1450

Reputation: 1651

Server Error in '/' Application. Resource Cannot Be Found. ASP.NET MVC

I'm using ASP.NET MVC 4 C Sharp and I have this error

Server Error in '/' Application.

The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /ClerkBooking/ConfirmBooking/22

In my controller I have:

  [HttpPost]
    [ValidateAntiForgeryToken]
    [Authorize(Roles = "Booking Clerk")]
    public ActionResult ConfirmBooking(int id = 0)
    {
       if (ModelState.IsValid)
       {
            //Find the booking
            Booking booking = db.Bookings.Find(id);

            //Get RoomID of Preferred Room.
            int roomId = Convert.ToInt32(db.Rooms.Find(booking.PreferredRoom));

            //Set RoomID of Booking.
            booking.RoomId = roomId;

            //Save Changes.
            db.SaveChanges();
        }

        return View("Index");
    }

So im not sure why its not finding the method even though its in the correct place. Any help would be great! Thanks!

Upvotes: 0

Views: 7039

Answers (3)

Bura Chuhadar
Bura Chuhadar

Reputation: 3751

Are adding your AntiForgeryToken to your html file?

@using (Html.BeginForm("Manage", "Account")) {
    @Html.AntiForgeryToken()
}

If not then probably asp.net mvc is blocking to reach your controller.

Also do not forget to check your Global.asax with the parameters:

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

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "ClerkBooking", action = "ConfirmBooking", id = UrlParameter.Optional } // Parameter defaults
            );

        }

Otherwise you have to declare your id object from outside.

 $.ajax("/ClerkBooking/ConfirmBooking/?id=22", {
            type: "POST",
            contentType: "application/json; charset=utf-8",
            success: function (result) {

               //Do Something

                }
            }
        }).fail(function () {
                         //Do Something
        });

Upvotes: 0

Zach
Zach

Reputation: 447

Make sure your controller is called "ClerkBooking" and remove the [HttpPost] decoration from the method.

Upvotes: 0

John Gibb
John Gibb

Reputation: 10773

Your action link @Html.ActionLink("Confirm Booking", "ConfirmBooking", new {id = booking.BookingId}) is going to make a GET request, but you put an [HttpPost] attribute on the action.

You'll probably want to make the link a button inside of a form post instead of an action link.

Here's an example:

@using (Html.BeginForm("ConfirmBooking", "ClerkBooking", new { id = booking.BookingId }))
{
    <input type="submit" value="Confirm Booking" />
}

Upvotes: 2

Related Questions