@vincent90
2016-01-19T02:15:12.000000Z
字数 6627
阅读 1687
WebAPI
ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework.
Controller | Action | Http Verb | URI |
---|---|---|---|
HTE | GetModel | GET | /HTE/GetModel/{id} |
HTE | GetAnswerCard | GET | /HTE/GetAnswerCard?hteId={hteId}&userId={userId} |
HTE | CreateFull | POST | /HTE/CreateFull |
HTE | Update | PUT | /HTE/Update/{id} |
HTE | Remove | DELETE | /HTE/Remove/{id} |
public class HTEController : ApiBaseController
{
}
Use Attribute([HttpGet]、[HttpPost]、[HttpPut]、[HttpDelete])
Define return model in webAPI,attribute name first letter should be lowercase.
public class HTEController : ApiBaseController
{
[HttpGet]
public HTEModel GetHTEModel(Int64 id)
{
HTEModel hTEModel = new HTEModel();
HTE hte = EntAppFrameWork.EntAppFrameWorkContext.DomainModelService.ExtenedService<IHTE>().GetHteByHteId(id);
hTEModel.HTEName = hte.Name;
hTEModel.Score = hte.Score.ToString();
hTEModel.HTEType = hte.HTEType;
hTEModel.StartTime = hte.PublishDateTime;
hTEModel.EndTime = hte.EndTime;
Collection<IncludeExerciseModel> exercises = new Collection<IncludeExerciseModel>();
if (hte.Exercises != null)
{
foreach (var item in hte.Exercises)
{
IncludeExerciseModel includeExerciseModel = new IncludeExerciseModel();
includeExerciseModel.ExerciseId = item.Exercise.Key;
includeExerciseModel.Score = item.Score.ToString();
includeExerciseModel.Sequence = item.Sequence;
exercises.Add(includeExerciseModel);
}
}
hTEModel.Exercises = exercises;
return hTEModel;
}
}
public class HTEModel
{
public string hTEName { get; set; }
public DateTime startTime { get; set; }
public DateTime endTime { get; set; }
public string score { get; set; }
public Collection<IncludeExerciseModel> exercises { get; set; }
public HTEType hTEType { get; set; }
}
Parameter Data Transfer Object
A DTO is a simple container for a set of aggregated data that needs to be transferred across a process or network boundary.
[HttpPost]
public Int64 CreateFullHTE([FromBody]CreateFullHTEDTO createFullHTEDTO)
{
var hteID = EntAppFrameWork.EntAppFrameWorkContext.DomainModelService.ExtenedService<IHTE>().CreateFullHTE(createFullHTEDTO.HteName,createFullHTEDTO.Generatetype,createFullHTEDTO.HTEType,createFullHTEDTO.Exercises,createFullHTEDTO.CourseId,createFullHTEDTO.UserId, createFullHTEDTO.StartTime, createFullHTEDTO.EndTime);
return hteID;
}
public class CreateFullHTEDTO
{
public string HteName { get; set; }
public GenerateType Generatetype { get; set; }
public HTEType HTEType { get; set; }
public List<IncludeExerciseDto> Exercises { get; set; }
public Int64 CourseId { get; set; }
public Int64 UserId { get; set; }
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
}
Security tokens are used to prove one's identity electronically. The token is used in addition to or in place of a password to prove that the customer is who they claim to be. The token acts like an electronic key to access something.
/Controller/Action?ssotoken=XXXX
[AllowAnonymousAttribute]
public class XXController : ApiBaseController
{
}
{
"ErrorCode":"300300010",
"Data":"",
"ErrorMessage":"Exercise not find"
}
In this article we will define how to add the Test Client to the ASP. NET Web API help page.
First we create the help page. For creating the help page use this link: Creating the Help Page in ASP. NET Web API.
Install the test client package using the following procedure:
When the Web API Test Client is installed, the following files will be added to your application:
Now write the Test Client on the help page.
For selecting the Api.cshtml, go to the Solution Explorer and choose "Areas\HelpPage\Views\Help\Display Templates\Api.cshtml".
We add the following code in the Api.cshtml file that is @Html.DisplayForModel("TestClientDialogs"). We can add this code after the
@using System.Web.Http
@using MvcApplication4.Areas.HelpPage.Models
@model HelpPageApiModel
@{
var description = Model.ApiDescription;
ViewBag.Title = description.HttpMethod.Method + " " + description.RelativePath;
}
<div id="body">
<section class="featured">
<div class="content-wrapper">
<p>
@Html.ActionLink("Help Page Home", "Index")
</p>
</div>
</section>
<section class="content-wrapper main-content clear-fix">
@Html.DisplayFor(m => Model)
</section>
</div>
@Html.DisplayForModel("TestClientDialogs")
@section Scripts {
<link type="text/css" href="~/Areas/HelpPage/HelpPage.css" rel="stylesheet" />
@Html.DisplayForModel("TestClientReferences")
}
Now we add some JavaScript files into the TestClientReferences.cshtml that is visible in the scripts folder.
We can drag and drop these files into the TestClientreferences.cshtml file. For selecting the TestClientReferences.cshtml. Go to Solution Explorer and choose
"Areas\HelpPage\Views\Help\Display Templates\TestClientReferences.cshtml".
The code looks like this:
@using System.Text.RegularExpressions
<script src="~/Scripts/jquery-1.8.2.js"></script>
<script src="~/Scripts/knockoutjs%202.1.0.js"></script>
<script src="~/Scripts/jquery-ui-1.10.3.js"></script>
<link href="~/Content/themes/base/jquery.ui.all.css" rel="stylesheet" />
<link href="~/Areas/HelpPage/TestClient.css" rel="stylesheet" />
@* Automatically grabs the installed version of JQuery/JQueryUI/KnockoutJS *@
@{var filePattern = @"(jquery-[0-9]+.[0-9]+.[0-9]+.js|jquery-ui-[0-9]+.[0-9]+.[0-9]+.js|knockout-[0-9]+.[0-9]+.[0-9]+.js)";}
@foreach (var item in Directory.GetFiles(Server.MapPath(@"~/Scripts")).Where(f => Regex.IsMatch(f, filePattern)))
{
<script src="~/Scripts/@Path.GetFileName(item)"></script>
}
<script src="~/Scripts/WebApiTestClient.js" defer="defer"></script>
If these files are not showing in Scripts folder then you can download these files.
After performing these steps, when we execute the application the Test API button is shown in lower-right corner. And the output looks like this:
Now we test the Web API.
Add the request header.
Click on "Add header" and add the "accept text/xml" asking for XML. Click on the "Send" button.
The response is: