asp.net-mvc - POST 请求后重定向时 MVC3 401 未经授权的响应

标签 asp.net-mvc json asp.net-mvc-3 http rest

我正在使用 ASP.NET MVC 3 编写 RESTful Web 服务。我正在使用基本的 GET/POST/PUT 映射分别进行检索/创建/更新操作。它非常适合我的一种域类型,但我正在为另一种类型实现完全相同的一组操作,并且我从本地 ASP.NET 开发服务器收到 401 未经授权的响应。

首先,这是 MyApiAreaRegistration.cs 中创建路由的 RegisterArea 方法的相关部分(为简洁起见,我删除了 Update 方法):

// student detail
context.MapRoute(
    "StudentDetailV1",
    "MyApi/v1/family/{email}/student/{id}",
    new { controller = "Student", action = "StudentDetail" },
    new { httpMethod = new HttpMethodConstraint("GET") }
);

// create student
context.MapRoute(
    "CreateStudentV1",
    "MyApi/v1/family/{email}/student",
    new { controller = "Student", action = "CreateStudent" },
    new { httpMethod = new HttpMethodConstraint("POST") }
);

// family detail
context.MapRoute(
    "FamilyDetailV1",
    "MyApi/v1/family/{email}",
    new { controller = "Student", action = "FamilyDetail" },
    new { httpMethod = new HttpMethodConstraint("GET") }
);

// create family
context.MapRoute(
    "CreateFamilyV1",
    "MyApi/v1/family",
    new { controller = "Student", action = "CreateFamily" },
    new { httpMethod = new HttpMethodConstraint("POST") }
);

在 StudentController 中:

/// <summary>
/// return json representation of FamilyDto
/// </summary>
/// <param name="email"></param>
/// <returns></returns>
[HttpGet]
public ActionResult FamilyDetail(string email)
{
    Family f = _studentDataAccess.GetFamilyByEmail(email.Trim());
    if (f == null)
    {
        return HttpNotFound();
    }

    FamilyDto familyDto = FamilyDtoAssembler.GetDtoFromFamily(f);

    return Json(familyDto, JsonRequestBehavior.AllowGet);
}

[HttpPost]
public ActionResult CreateFamily(FamilyDto familyDto)
{
    if (string.IsNullOrWhiteSpace(familyDto.Email))
    {
        return new HttpStatusCodeResult(409, "Email address is required.");
    }

    // check for already existing family with that email.
    Family f = _studentDataAccess.GetFamilyByEmail(familyDto.Email.Trim());
    if (f != null)
    {
        return new HttpStatusCodeResult(409, "Email address must be unique.");
    }

    // turn family into a Family object with Parents
    Family family = FamilyDtoAssembler.GetFamilyFromDto(familyDto);

    // save family via dal (username = family email)
    _studentDataAccess.CreateFamily(family, family.Email);

    // return redirect to family detail
    return RedirectToRoute("FamilyDetailV1", new { email = family.Email });
}


[HttpPost]
public ActionResult CreateStudent(string email, StudentDto studentDto)
{
    if (string.IsNullOrWhiteSpace(email))
    {
        return HttpNotFound();
    }

    Family family = _studentDataAccess.GetFamilyByEmail(email.Trim());

    if (family == null)
    {
        return HttpNotFound();
    }

    Student s = StudentDtoAssembler.GetStudentFromDto(_repository, studentDto);

    s.Family = family;
    _studentDataAccess.CreateStudent(s, email);

    return RedirectToRoute("StudentDetailV1", new { email = email, id = s.Id });
}

[HttpGet]
public ActionResult StudentDetail(string email, int id)
{
    Student s = _studentDataAccess.GetStudent(id);
    if (s == null || s.Family.Email != email)
    {
        return HttpNotFound();
    }

    StudentDto studentDto = GeneralDtoAssembler.GetSingleStudentDto(s, s.MatsRegistrations);

    return Json(studentDto, JsonRequestBehavior.AllowGet);
}

现在,当我使用 Fiddler 为“Create family”操作创建一个 POST 请求时,使用正确的 JSON 请求正文来匹配 Family DTO 的定义,该操作返回一个 302 响应,该响应重定向到 Family Detail 操作新创建的家庭。这正是我想要的。但是问题是“创建学生”操作。当我在调试器中逐步执行它时,它可以正常工作并返回 RedirectToRoute 结果,但是我在 Fiddler 中看到的只是以下响应:

HTTP/1.1 401 Unauthorized
Server: ASP.NET Development Server/10.0.0.0
Date: Tue, 17 Apr 2012 16:42:10 GMT
X-AspNet-Version: 4.0.30319
jsonerror: true
Cache-Control: private
Content-Type: application/json; charset=utf-8
Content-Length: 105
Connection: Close

{"Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"}

我已经尝试了所有我能想到的方法,从将 web.config 中的身份验证模式从“Windows”更改为“无”,到重新排序路由,再到返回 RedirectToAction 而不是 RedirectToRoute,但都没有任何效果。请注意,如果我只是从 CreateStudent 直接返回 Json 结果而不是 RedirectToRoute 结果,那么它会正常工作(200 状态,我看到结果很好)。但是,我不明白为什么 CreateStudent 和 CreateFamily 的行为不同,这让我抓狂了 2 天。哈尔普?

最佳答案

问题出在学生 route 的静态 URL 段。我将 StudentDetailV1 更改为 "MyApi/v1/family/{email}/{id}",现在它可以正常工作了。

关于asp.net-mvc - POST 请求后重定向时 MVC3 401 未经授权的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10195716/

相关文章:

c# - ASP.NET MVC 5 Url 重写以包含 Controller 中的所有操作?

php - 编码 php postgres json_encode

javascript - 使用 JavaScript 和 JQuery 带有新对象的空 JSON 字符串

c# - asp.net identity 获取登录用户的所有角色

c# - Razor 语法在 MVC 5 View VS2013 上损坏

Javascript nodejs JSON.parse 数组

c# - 重定向到 URL - asp.net 与 MVC

asp.net-mvc - 如何在网络服务器(MVC)中上传文件时生成唯一文件名

ajax - ASP.NET MVC 3 如何在创建 View 中实现多对多关系

c# - 在 CustomValidation 中返回一个复杂对象而不是 ValidationResult