我使用ASP.NET MVC和WebAPI相对较新,我正在重写我的网站,然后不再使用PHP。
我有一个用户可以添加新内容的页面,即测验密码。使用PHP和jQuery我有一个页面,我将使用jQUERY $ .ajax发布测验密码的详细信息。我以前用以下字段返回JSON:
我决定在添加,更新和删除测验密码时,我会使用WebAPI控制器。
如果有的话,我已经返回了Success BOOL和ErrorMessage值,但是我不知道如何使用我使用部分视图的转义HTML来填充Html
值用于页面上的项目。
到目前为止,这是我的WebAPI控制器:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using uQuiz.Domain.Abstract;
using uQuiz.Domain.Models;
using uQuiz.WebUI.Models.Api;
using Microsoft.AspNet.Identity;
using uQuiz.Domain.Helpers;
namespace uQuiz.WebUI.Controllers
{
[Authorize]
public class PasswordController : ApiController
{
private IQuizEntities Context;
public PasswordController(IQuizEntities context)
{
this.Context = context;
}
// GET: api/Password/5
public object Get(int id)
{
// Check that quiz password exists
if (this.Context.EntitySet<QuizPassword>().Any(x => x.Id == id))
{
QuizPassword quizPassword = this.Context.EntitySet<QuizPassword>().Where(x => x.Id == id).FirstOrDefault<QuizPassword>();
ApiQuizPassword model = new ApiQuizPassword()
{
Id = quizPassword.Id,
Password = quizPassword.Password
};
return model;
}
else
{
return null;
}
}
// POST: api/Password
public ApiResult Post([FromBody]ApiQuizPassword apiQuizPassword)
{
Quiz quiz = this.Context.EntitySet<Quiz>().Find(apiQuizPassword.QuizId);
int userId = User.Identity.GetUserId<int>();
ApiResult result = new ApiResult();
if (!ModelState.IsValid)
{
// Return the first validation result
result.Success = false;
result.ErrorMessage = ModelState.First().Value.Errors[0].ErrorMessage;
return result;
}
if (!quiz.IsQuizOwner(userId))
{
result.Success = false;
result.ErrorMessage = "Logged in user is not quiz owner";
return result;
}
if (quiz.DoesPasswordAlreadyExistForQuiz(apiQuizPassword.Password))
{
result.Success = false;
result.ErrorMessage = "Password already exists for this quiz";
return result;
}
using (var transaction = this.Context.Database.BeginTransaction())
{
try
{
QuizPassword quizPassword = this.Context.EntitySet<QuizPassword>().Create();
// Assign posted values
quizPassword.Password = apiQuizPassword.Password;
quizPassword.QuizId = apiQuizPassword.QuizId;
// Assign default values
quizPassword.RemainingUses = 0;
quizPassword.UnlimitedUses = true;
quizPassword.UseCount = 0;
quizPassword.Deleted = false;
quizPassword.CreatedTime = TimeHelper.UnixTimestamp;
this.Context.EntitySet<QuizPassword>().Add(quizPassword);
this.Context.SaveChanges();
transaction.Commit();
result.Success = true;
return result;
}
catch (Exception)
{
transaction.Rollback();
result.Success = false;
result.ErrorMessage = "Failed to save the quiz password";
return result;
}
finally
{
transaction.Dispose();
}
}
}
// PUT: api/Password/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE: api/Password/5
public void Delete(int id)
{
using (var transaction = this.Context.Database.BeginTransaction())
{
try
{
QuizPassword quizPassword = this.Context.EntitySet<QuizPassword>().Find(id);
this.Context.Entry(quizPassword).State = EntityState.Deleted;
this.Context.SaveChanges();
transaction.Commit();
}
catch (Exception)
{
transaction.Rollback();
}
finally
{
transaction.Dispose();
}
}
}
}
}
这是我从控制器中的Post
方法返回的API结果类:
public class ApiResult
{
public bool Success { get; set; }
public string ErrorMessage { get; set; }
public HtmlString Html { get; set; }
}
我的部分视图称为_QuizPasswordPartial
。
我是否完全错过了标记,我应该只从我的Get
方法返回HTML吗?
无论哪种方式,我都希望在JSON结果中从控制器返回我的渲染部分。
答案 0 :(得分:0)
Web API返回数据(JSON / XML)而不是呈现的视图(HTML)。使用Javascript调用API,检索原始数据并更新浏览器中的视图。