DevT
DevT

Reputation: 4933

MVC 4- Server Error in '/' Application

I got following error and i can't find any reason for this. I have view in correct folder as well.

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: /Account/GetUsers

My controller class code as below: i'm trying to get list of users in database and display.

 public class AccountController : Controller
    {

        [AllowAnonymous]
        [HttpPost]
        public ActionResult GetUsers()
        {
            var availableProfiles = ProfileManager.GetAllProfiles(ProfileAuthenticationOption.All);
// havent implement yet...
            return View();
        }
}

My view is as below:

@model IEnumerable<LibraryManagementWeb.Models.StaffModel>

@{
    ViewBag.Title = "GetUsers";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="row">
    <div class="col-lg-12">
        <div class="box">
            <div class="box-header" data-original-title="">
                <h2>
                    <i class="icon-user"></i>
                    <span class="break"></span>
                    Staff user Details
                </h2>
                <div class="box-icon">
                </div>
            </div>
            <div class="box-content">
                              <fieldset class="col-sm-12">
                            <legend></legend>      


                 <a href="@Url.Action("RegisterStaf", "Account")" class="btn btn-success"><i class="icon-zoom-in "></i> <span>Add</span></a>


</fieldset>                   

            </div>
        </div>
    </div>
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

my folder hierarchy as below:

Image

I call this view in my _layout.cshtml as follows:

                @if (User.IsInRole("Admin"))
                {
                    <li><a href="@Url.Action("GetUsers", "Account")"><i class="icon-user"></i><span class="hidden-sm">User Management</span></a></li>
                }

What can be the reason for this?

Edit: My route details are as follows:

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

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

Upvotes: 2

Views: 5260

Answers (1)

Andrew Barber
Andrew Barber

Reputation: 40139

I suspect I know what your problem is. In this code:

public class AccountController : Controller
    {

        [AllowAnonymous]
        [HttpPost]
        public ActionResult GetUsers()
        {
            var availableProfiles = ProfileManager.GetAllProfiles(ProfileAuthenticationOption.All);
// havent implement yet...
            return View();
        }
}

Note the [HttpPost] attribute. This causes that action method only to match when the page is submitted via POST - it won't match for GET requests. If you do not have a corresponding action method which accepts GET requests, you will get the 404 error.

From what you have in the method, it doesn't look like you need the [HttpPost] at all, anyway. Try removing that attribute, and try again.

Upvotes: 3

Related Questions