Mvc web api automatically generates a document, mvcapi

Source: Internet
Author: User

Mvc web api automatically generates a document, mvcapi

Recently, webapis have been used as interfaces for mobile terminals. But every time I add or modify interfaces, I need to modify the document again, which makes me very worried. Accidentally discovered. Webapi has the function of automatically generating documents .... I saw the savior.

After reading some documents, I found that if your development environment is old, you may need to manually install a new component in nuGet, such as VS2010 VS2008,

You need to install this component for configuration. After the installation is complete, there will be one more folder (because this version may have a dependency version conflict)

If you are using version 2013, you will have this folder by default when creating a project. Of course, I mean creating a webapi project.

Here I will focus on the following class, which is used to configure the Help Page.

Next, let's explain our configuration step by step.

First, you need to uncomment the first line of code.

Using System; using System. collections. generic; using System. net. http. headers; using System. web; using System. web. http; namespace Cidtech. faceProject. areas. helpPage {// <summary> // Use this class to customize the Help Page. /// For example you can set a custom <see cref = "System. web. http. description. IDocumentationProvider "/> to supply the documentation // or you can provide the samples for the reques Ts/responses. /// </summary> public static class HelpPageConfig {public static void Register (HttpConfiguration config) {// This is the first step to set the document generation path, first, you uncomment this line of code and write down the default path config. setDocumentationProvider (new XmlDocumentationProvider (HttpContext. current. server. mapPath ("~ /App_Data/XmlDocument. xml "); // Uncomment the following to use" sample string "as the sample for all actions that have string as the body parameter or return type. /// Also, the string arrays will be used for IEnumerable <string>. the sample objects will be serialized into different media type // formats by the available formatters. the format parameter is disabled by default. // config. setSampleObjects (new Dictionary <Type, object> // {typeof (string), "sample string"}, // {typeof (IEnumerable <string> ), new string [] {"sample 1", "sample 2 "}}//}); /// Uncomment the following to use "[0] = foo & [1] = bar" directly as the sample for all actions that support form URL encoded format // and have IEnumerable <string> as the body parameter or return type. // config. setSampleForType ("[0] = foo & [1] = bar", new MediaTypeHeaderValue ("application/x-www-form-urlencoded "), typeof (IEnumerable <string> )); /// Uncomment the following to use "1234" directly as the request sample for media type "text/plain" on the controller named "Values" // and action named" put ". // config. setSampleRequest ("1234", new MediaTypeHeaderValue ("text/plain"), "Values", "Put"); // Uncomment the following to use the image on ".. /images/aspNetHome.png "directly as the response sample for media type" image/png "// on the controller named" Values "and action named" Get "with parameter" id". // config. setSampleResponse (new ImageSample (".. /images/aspNetHome.png "), new MediaTypeHeaderValue (" image/png ")," Values "," Get "," id "); /// Uncomment the following to correct the sample request when the action expects an HttpRequestMessage with ObjectContent <string>. /// The sample will be generated as if the controller named "Values" and action named "Get" were having string as the body parameter. // config. setActualRequestType (typeof (string), "Values", "Get"); // Uncomment the following to correct the sample response when the action returns an HttpResponseMessage with ObjectContent <string>. /// The sample will be generated as if the controller named "Values" and action named "Post" were returning a string. // config. setActualResponseType (typeof (string), "Values", "Post ");}}}

Step 2: set the path for generating XML in your project.

 

The two paths must be the same.

In fact, this part is basically completed. Run the project if you want to see the effect.

The description here is actually the annotation you wrote in the api

Here. The entire process is complete. If you need simple configuration, you can use it. After taking the time, I will explain the entire program running process in detail. Here I will explain it in detail. If you do not understand it, you can trust me.

 


Demonstrate how to upload a file MVC40 through web api

WebApiWebFormHost/UploadFileController. cs/** upload files through web APIs */using System; using System. collections. generic; using System. linq; using System. net; using System. net. http; using System. threading. tasks; using System. web. http; namespace MVC40.Controllers {publicclass UploadFileController: ApiController {publicasync Task <string> Post () {// check whether multipart/form-dataif (! Request. content. isMimeMultipartContent ("form-data") thrownew HttpResponseException (HttpStatusCode. unsupportedMediaType); // sets the upload directory var provider = new MultipartFormDataStreamProvider (@ "c: \ temp"); // receives data and saves the file var bodyparts = await Request. content. readAsMultipartAsync (provider); string result = ""; // obtain the form data result + = "formData txtName:" + bodyparts. formData ["txtName"]; result + = "<br/>"; // get the number of files Result + = "fileData headers:" + bodyparts. fileData [0]. headers; // result + = "<br/>"; result + = "fileData localFileName:" + bodyparts. fileData [0]. localFileName; // The address for saving the file on the server. If necessary, rename or movereturn result ;}} WebApiWebFormHost/UploadDemo. cshtml @ {Layout = null ;}<! DOCTYPE html> PHP, Hypertext Preprocessor, was first launched in 95 years (formerly called Personal Home Page ). PHP was originally a server-side scripting language for creating dynamic web pages, but now PHP can run the GUI standalone program as command line. Currently, PHP Group is developing and managing PHP rules and functions. However, PHP is mostly used in dynamic webpage scripting. It seems that no one has ever heard of using it to develop desktop applications.

PHP usage is the highest among several languages for creating dynamic web pages (JSP, ASP, ASP. NET, ColdFusion, Ruby, and SHTML. Since PHP has been available in 95 years, there are a considerable number of developers. We are familiar with the international authoritative encyclopedia Wikipedia, which is written in PHP 90%. In addition, FaceBook, which has been popular in foreign countries for many years, is also written in PHP. At the same time, Yahoo, Sony official, pepsi officially uses PHP for these websites. According to Wikipedia, PHP is used by more than 20,000,000 of websites and more than 1,000,000 of web servers. Currently, the latest PHP version is 5.2.6 in 2008.

PHP is not only highly used, but also compatible with various platforms. It can run on most Web servers (such as ms iis and Apache), and on most operating systems (such as Windows, UNIX, and Linux ). PHP also supports many brands of databases: MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Gerneric ODBC, etc.

In terms of structure, PHP has many frameworks available even though it is worse than JSP and ASP. NET. Microsoft's ASP. NET structure is famous, and the MVC structure of JSP is also very powerful. Therefore, ASP. NET and JSP are very fast to develop large websites. PHP Group has made great efforts to develop its own special structure to meet the needs of different types of large websites. Common PHP frameworks include CakePHP, PRADO, Symfony, and Zend.

In addition, the LAMP structure is the most common development mode for PHP, that is, it works with Linux, Apache, MySQL, PHP, and LAMP. I personally think this method is the simplest in ASP. NET, PHP, and JSP.

3. JSP. (JSP is the most used in my work)
JSP and JavaServer Pages are part of Java technology and can be said to be part of Java Platform Enterprise Edition (JAVA mainly involves three major technologies: Java SE, Java EE, and Java ME ). Software developers can use JSP to dynamically generate HTML, XML, or other documents. Then, send the document response to the webpage user. This technology supports all defined libraries in Java, so JSP APIs are the same as ordinary Java EE APIs.

JSP also contains many functional XML tags (such as <jsp: include> and <jsp: useBean ). These are built-in JAVA functions and do not need to be imported into the new External library. These XML tags can be used in many Web servers (such as Apache, Apache TomCat, and WebLogic ).

JSP is exactly the same as Servlet after compilation. When the JSP page is accessed by the user for the first time, it is first compiled into a JAVA Servlet Class by the Java compiler. Then, the compiled Servlet Class will run directly in JVM when it is accessed by users in the future. The full text is provided by JS ......>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.