ASP MVC 5,找不到路由

时间:2018-04-15 09:34:44

标签: c# .net asp.net-mvc asp.net-mvc-routing

使用名为routing和标准传统路由的属性混合。

我有一个UserController有两个'创建'动作。一个是用于获取视图的HTTPGet,一个是用于发回数据的HTTPPost

这是整个控制器:

[RoutePrefix("User")]
public class UserController : Controller {
    public UserController() {}

    [HttpGet]
    public ActionResult Create() {
        return View();
    }

    [Route("Create")]
    [HttpPost]
    public async Task<ActionResult> CreateAsync(UserM m) {
        if (!ModelState.IsValid) {
            return View(m);
        }

        var user = new User() {
            Email = m.Email,
            Forename = m.Forename,
            IsActive = true,
            Surname = m.Surname
        };


        return Redirect("");    
    }
}

但是,当我尝试从网站上的提交按钮导航到用户/创建时,要发回数据,我会收到404。

这是路线配置:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    // Turn on attribute routing in the controllers
    routes.MapMvcAttributeRoutes();

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

以下是发布数据的视图:

@model MyApp.WebMS.Models.UserM
@{
    ViewBag.Title = "Create User";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="container">

    <div class="row">
        <div class="xs-12">
            <h3>Create User</h3>
            <hr class="no-top-margin" />
        </div>
    </div>

    @using (Html.BeginForm()) {
        <div class="row">
            <div class="col-sm-4">
                <h4>Personal Information</h4>
                <div class="user-creation-section">
                    <div class="form-group">
                        @Html.LabelFor(m => m.Forename, new { @class = "form-label" })
                        @Html.TextBoxFor(m => m.Forename, new { @class = "form-control" })
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(m => m.Surname, new { @class = "form-label" })
                        @Html.TextBoxFor(m => m.Surname, new { @class = "form-control" })
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(m => m.Email, new { @class = "form-label" })
                        @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
                    </div>
                </div>
            </div>
            <div class="col-sm-4">
                <h4>Job Information</h4>
                <div class="user-creation-section">
                    <div class="form-group">
                        @Html.LabelFor(m => m.JobTitle, new { @class = "form-label" })
                        @Html.TextBoxFor(m => m.JobTitle, new { @class = "form-control" })
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(m => m.StartDate, new { @class = "form-label" })
                        @Html.TextBoxFor(m => m.StartDate, new { type = "date", @class = "form-control" })
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="xs-12 pull-right">
                <input type="button" class="btn" value="Back" onclick="location.href='@Url.Action("Index", "Home")'" />
                <button type="submit" class="btn btn-primary">Create</button>
            </div>
        </div>
    }
</div>

1 个答案:

答案 0 :(得分:0)

由于您的POST操作名为CreateAsync,因此您必须在表单中明确提及。

@using (Html.BeginForm("CreateAsync", "User", FormMethod.Post))