'/'应用程序中的MVC 4-服务器错误

时间:2013-12-04 05:31:46

标签: asp.net-mvc-4 razor

我收到了以下错误,我找不到任何理由。我也在正确的文件夹中查看。

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

我的控制器类代码如下:我正在尝试获取数据库中的用户列表并显示。

 public class AccountController : Controller
    {

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

我的观点如下:

@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")
}

我的文件夹层次结构如下:

Image

我在_layout.cshtml中将此视图称为如下:

                @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>
                }

这可能是什么原因?

修改:我的路线详情如下:

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 }
            );
        }
    }

1 个答案:

答案 0 :(得分:3)

我怀疑我知道你的问题是什么。在这段代码中:

public class AccountController : Controller
    {

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

请注意[HttpPost]属性。这会导致该操作方法仅在通过POST提交页面时进行匹配 - 它与GET请求不匹配。如果您没有接受GET请求的相应操作方法,则会收到404错误。

根据你在方法中所拥有的,无论如何你看起来根本不需要[HttpPost]。尝试删除该属性,然后重试。