我正在使用ASP.NET MVC 3编写RESTful Web服务。我分别使用基本GET / POST / PUT映射进行检索/创建/更新操作。它适用于我的一个域类型,但我正在为另一种类型实现完全相同的一组操作,并且我从本地ASP.NET开发服务器获得401 Unauthorized响应。
首先,这是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为“创建系列”操作制作POST请求时,如果正确的JSON请求主体与Family DTO的定义相匹配,则操作将返回302响应,该响应将重定向到“系列详细信息”操作新创建的家庭。这正是我想要的。然而,问题是“创造学生”行动。当我在调试器中单步执行它时,它可以正常工作并返回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天了。公顷; lp的
?答案 0 :(得分:0)
问题是学生路线中的静态网址段。我将StudentDetailV1
更改为"MyApi/v1/family/{email}/{id}"
,现在它正常运行。