ASP. NET MVC4 WEBAPI application Client Access server side

Source: Internet
Author: User

I. Description

Client project type is designed as: WinForm (WinForm form project type)

The server-side project type is designed as: ASP. Mvc4 WEBAPI

This is divided into two scenarios: project run and Debug.

Run:

This situation refers to the server-side project has been developed, you can deploy it to IIS (http://localhost:8748) in parentheses is the server-side deployment to IIS access address, the client access to the server to use

Debugging:

When debugging, you can create the client and server side under the same solution. Debugging steps: Run the server project first, then check the server's access address in the tray

Assign the server access address to my client, run the Client Access server side.

Ii. creating a server-side project

First open the VS2012 development environment software,

1. Create a Blank Solution

Steps: File-new-project-Other project Types-Visual Studio Solution

The solution is named: MyTest

2. Create server-side

Right-click the solution created in the previous step-add-New project-"Web-->asp.net MVC4 Web application (named: MyServer)--" Web API (MVC project template)

3. Add WEBAPI Controller

Right-click the Controllers folder in the MyServer project-"Add-" controller (named User)--"Empty API Controller"

Adding a method to a controller

[HttpGet]

public string GetUserInfo (string userName, String PassWord)

{

if (UserName = = "Admin" && PassWord = = "123456")

{

Return "Success";

}

Else

{

Return "Failed";

}

}

Also add custom routing rules in file Webapiconfig

Config. Routes.maphttproute (

Name: "MyApi",

Routetemplate: "Api/{controller}/{action}/{key}",

defaults:new {key = routeparameter.optional}

);

Iii. Creating clients (client)

Right-click the solution "MyTest"-"Add-" New project-"windows-" Windows Forms Application (named: myclient)

The design interface on the default form Form1 is as follows:

The background code for form Form1 is as follows:

Using System;

Using System.Collections.Generic;

Using System.ComponentModel;

Using System.Data;

Using System.Drawing;

Using System.Linq;

Using System.Net.Http;

Using System.Text;

Using System.Threading.Tasks;

Using System.Windows.Forms;

Namespace MyClient

{

public partial class Form1:form

{

Public Form1 ()

{

InitializeComponent ();

}

private void btnSubmit_Click (object sender, EventArgs e)

{

String userName = TxName.Text.Trim ();

String PassWord = TxPwd.Text.Trim ();

String url = @ "http://localhost:47673/api/File/GetUserInfo?userName=" + userName + "&password=" +password;

HttpClient client = new HttpClient ();

Httpresponsemessage response = client. Getasync (URL). Result;

String str= Response. Content.readasstringasync (). Result;

MessageBox.Show (str);

}

}

}

Note In the client-side reference assembly System.Net.Http; Reference method: Right-click the Reference folder in MyClient-"Add Reference-" assembly

The using System.Net.Http is also added in the background code of Form1;

Four, commissioning

1, in vs2012, select the server-side project, after the compilation passed, "Start execution (not debugging)"

2, view the server's access address, such as

Right-click IIS Express in the Tray

Replace the URL of the access address in the client backend code with:

String url = @ "http://localhost:8748/api/User/GetUserInfo?userName=" + userName + "&password=" +password;

3. Start the client project

In the solution, select the client project (set as Startup Project), MyClient start debugging

ASP. NET MVC4 WEBAPI application Client Access server side

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.