Using Owin as the host for Webapi

Source: Internet
Author: User

Preface

Well, there is nothing to say, is a technical summary, directly generated MVC project, feel good heavy, although a variety of things very full ... Maybe I'm a virgo? - -,

Owin Well, here I do not explain, oneself is also smattering, can refer

Open Web Interface for. NET

Here we go...

First look at the results.

Entire Web project ... Very refreshing. --I think so.

Body Start

First we create an empty Web project:

After the creation is complete, there is nothing in it, and then we add a Owin startup class as follows:

Write the code in our start up as follows (the explanation I wrote in the note):

usingSystem;usingSystem.Threading.Tasks;usingMicrosoft.owin;usingOwin;usingSystem.Web.Http; [Assembly:owinstartup (typeof(Owin_test.mystartup))]namespaceowin_test{ Public classMystartup { Public voidConfiguration (Iappbuilder app) {//Create an instance configuration of HTTPhttpconfiguration config =Newhttpconfiguration (); //Map RoutesCONFIG. Routes.maphttproute (Name:"Defaultapi", Routetemplate:"Api/{controller}/{id}", defaults:New{id =routeparameter.optional}); //inject the configuration into the Owin pipelineapp.         Usewebapi (config); }    }}

This step has a point, that is the app. Usewebapi (config) This method will prompt for missing, such as:

That's because we're missing this package. Microsoft ASP Web API 2 OWIN self-host

We open the NuGet console. Input command: Install-package Microsoft.AspNet.WebApi.OwinSelfHost

Wait for the download and installation to complete. The effect is as follows:

Below we write a WEBAPI controller, the code is as follows (the code is very simple, I will not explain):

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Net;usingSystem.Net.Http;usingSystem.Web.Http;namespaceowin_test.controller{ Public classValuescontroller:apicontroller {[HttpGet] Public stringDogettime (stringname) {            returnName + DateTime.Now.ToString ("YYYY-MM-DD"); }    }}

Write HTML, here we use AJAX access. The code is as follows:

<!DOCTYPE HTML><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"/>    <title></title>    <Scriptsrc= "Js/jquery/jquery-1.9.1.min.js"></Script>    <Script>        $(function () {            $("#testbtn"). Click (function() {$.get ("Api/values/dogettime", {name: $ ("#name"). Val ()},function(data) {alert (data); })            })        })    </Script></Head><Body>    <inputtype= "text"ID= "Name"/>    <inputID= "Testbtn"type= "button"value= "Call Webapi"/></Body></HTML>

The effect is as follows:

We got the data we wanted.

written in the last

Creating a WEBAPI project directly in a Web project can actually be achieved quickly,.. Individuals do not like to bring their own webapi template feel very painful add a lot of unnecessary things, the use of Owin when the host can be set up the project in the service, console, of course, you use MVC6 even. Can be deployed across platforms ~ ... Very convenient ... Article ends.

The shortcomings of the great God to ask you a lot of advice.

Using Owin as the host for Webapi

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.