用的是4.5的.net版本

构建model

    /// <summary>
/// 通过id获取资料
/// </summary>
//[Route("/GetStudentInfo", "POST")]
public class StudentInfoRequest : IReturn<StudentInfo>
{
public int id { get; set; }
} /// <summary>
/// 获取所有用户信息
/// </summary>
[Route("/GetAllStudentInfo", "POST")]
public class GetAllStudentInfoRequest : IReturn<StudentInfo>
{ } public class StudentInfo
{
public int id { get; set; }
public string name { get; set; }
public int age { get; set; }
public string grade { get; set; }
}

添加mvc web应用,创建service

public interface IStudentService
{
int Post(StudentInfo model);
StudentInfo Get(StudentInfoRequest request);
bool Delete(StudentInfoRequest request);
List<StudentInfo> Any(GetAllStudentInfoRequest request);
bool Put(StudentInfo model);
} public class StudentService : Service, IStudentService
{
private static List<StudentInfo> data = new List<StudentInfo>(); public int Post(StudentInfo model)
{
if (data.Count <= )
{
model.id = ;
data.Add(model);
}
else
{
model.id = data.Select(it => it.id).Max() + ;
data.Add(model);
}
return model.id;
} public bool Delete(StudentInfoRequest request)
{
var selectData = data.Where(it => it.id == request.id).FirstOrDefault();
if (selectData == null)
{
return false;
}
else
{
data.Remove(selectData);
return true;
}
} public StudentInfo Get(StudentInfoRequest request)
{
return data.Where(it => it.id == request.id).FirstOrDefault();
} public List<StudentInfo> Any(GetAllStudentInfoRequest request)
{
return data;
} public bool Put(StudentInfo model)
{
var selectData = data.Where(it => it.id == model.id).FirstOrDefault();
if (selectData == null)
{
data.Add(model);
}
else
{
data.Remove(selectData);
data.Add(model);
}
return true;
}
}

web全局文件配置

protected void Application_Start()
{
new APIServiceHost().Init();
} public class APIServiceHost : AppHostBase
{
//Register your web service with ServiceStack.
public APIServiceHost()
: base("API服务", typeof(APIServiceHost).Assembly)
{
//Routes.Add(typeof(int), "/Student", "POST", "添加学生信息", "");
//Routes.Add(typeof(bool), "/Student", "DELETE", "删除学生信息", "");
//Routes.Add<StudentInfo>("/Student", "GET");
//Routes.Add<List<StudentInfo>>("/Student", "GET");
//Routes.Add<bool>("/Student", "PUT"); //Routes.Add<int>("/Grade", "POST")
// .Add<bool>("/Grade/{Id}", "DELETE")
// .Add<GradeInfo>("/Grade/{Id}", "GET")
// .Add<List<GradeInfo>>("/Grade", "GET")
// .Add<bool>("/Grade", "PUT"); //启用请求参数合法性验证功能:
Plugins.Add(new ValidationFeature()); //Plugins.Add(new ProtoBufFormat()); //JsConfig.EmitCamelCaseNames = false;
//Plugins.Add(new SwaggerFeature());
Plugins.Add(new CorsFeature("*.test.com"));
} public override void Configure(Funq.Container container)
{
//Register any dependencies your services use here.
}
}

web.config的配置文件必须加上  红色标记的路径

<system.webServer>
<modules>
<remove name="TelemetryCorrelationHttpModule" />
<add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" preCondition="integratedMode,managedHandler" />
<remove name="ApplicationInsightsWebTracking" />
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
</modules> <validation validateIntegratedModeConfiguration="false" />
<handlers>
<!--<add path="*.aspx" name="DefaultHttpApplication" type="System.Web.UI.PageHandlerFactory" verb="*" ></add>-->
<add path="*" name="ServiceStack.Factory" type="ServiceStack.HttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" />
</handlers>
</system.webServer>

然后对接服务

 protected const string listenOnUrl = "http://localhost:2285/";//web的连接

对接接口
StudentInfo model = new StudentInfo();
GetStudent(model);
using (JsonServiceClient client = new JsonServiceClient(listenOnUrl))
{
model.id = client.Post<int>(model);
}

启动后的样子

c# ServiceStack web 搭建的更多相关文章

  1. ServiceStack Web Service 创建与调用简单示列

    目录 ServiceStack 概念 ServiceStack Web Service 创建与调用简单示列 上篇文章介绍了ServiceStack是什么,本章进入主题,如何快速简单的搭建Service ...

  2. IIS web搭建之虚拟主机

    IIS web搭建之虚拟主机 虚拟目录:能将一个网站的文件分散存储在同一个计算机的不同目录和其他计算机. 使用虚拟目录的好处: 1.将数据分散保存到不同的磁盘或者计算机上,便于分别开发和维护. 2.当 ...

  3. Shiro的原理及Web搭建

    shiro(java安全框架) 以下都是综合之前的人加上自己的一些小总结 Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码学和会话管理.使用Shiro的易于理解的A ...

  4. Tomcat WEB搭建+Nginx负载均衡动静分离+DNS解析的实验

    实验拓扑图: 实验环境: 在VMware workstation搭建虚拟环境,利用网络适配器的Nat和桥接模式模拟内网和外网环境. 实验过程中需要安装的工具包包括:vim unzip lrzsz ls ...

  5. Flask 使用pycharm 创建项目,一个简单的web 搭建

    1:新建项目后 2:Flask web 项目重要的就是app 所有每个都需要app app=Flask(__name__)   3:Flask 的路径是有app.route('path')装饰决定, ...

  6. pycharm环境下用Python+Django开发web搭建

    1.安装pycharm: 2.安装Python: 3.安装mysql: 4.安装Django; pip3 install django 5.创建Django工程命令方式: # 创建Django程序 d ...

  7. 使用 ServiceStack 构建跨平台 Web 服务

    本文主要来自MSDN杂志<Building Cross-Platform Web Services with ServiceStack>,Windows Communication Fou ...

  8. 使用 ServiceStack 构建跨平台 Web 服务(转)

    出处:http://www.cnblogs.com/shanyou/p/3348347.html 本文主要来自MSDN杂志<Building Cross-Platform Web Service ...

  9. Win7下Python WEB环境搭建

    环境介绍: Win7 64位 SP1 Python:2.7.6 网关接口:flup Nginx安装:http://blog.csdn.net/jacson_bai/article/details/46 ...

随机推荐

  1. vue 中引入第三方js库

    以 jQuery 为例 一.绝对路径直接引入,全局可用 主入口页面 index.html 中用 script 标签引入: <script src="./static/jquery-1. ...

  2. 连接Android模拟器

    一.如何找到adb?    安装夜神安卓模拟器后,电脑桌面会有“夜神模拟器”的启动图标,鼠标右键--打开文件所在的位置,就会进入***\Nox\bin,默认路径是C:\Program Files (x ...

  3. PostgreSQL SELECT INTO和INSERT INTO SELECT 两种表复制语句

    SELECT INTO和INSERT INTO SELECT两种表复制语句都可以用来复制表与表之间的数据,但是它们之间也有区别. 建表语句: bas_custom_rel表 CREATE TABLE ...

  4. Change Assembly Version in a compiled .NET assembly

    Change Assembly Version in a compiled .NET assembly You can use ILMerge: ILMerge.exe Foo.dll /ver:1. ...

  5. 如何捕捉Desried Capabilities中的appPackafe和appActive

    捕捉这两个参数需要借助adb工具的日志进行分析.ADB是一种命令行工具,用于PC和Android模拟器之前连接通信,集成在Android ADK中,默认在platfrom-tools目录下.在cmd运 ...

  6. 多进程之间的互斥信号量的实现(Linux和windows跨平台)

    最近工作中遇到了一些关于文件读取权限的问题.当一个程序中对一个固定名称的文件做了读写的操作的时候,外界通过并发式的调用这个应用的时候,可能存在多个进程同时去操作这个文件,这个时候可能会造成调用失败的问 ...

  7. Sql中truncate,delete以及drop的比较

    相同点: 1.truncate和不带where子句的delete.以及drop都会删除表内的数据. 2.drop.truncate都是DDL语句(数据定义语言),执行后会自动提交. 不同点: 1. t ...

  8. P3015 [USACO11FEB]最好的括号Best Parenthesis

    P3015 [USACO11FEB]最好的括号Best Parenthesis 题解 一定要开 long long !!! 通过阅读英文题面我们知道所给出的字符串是已经匹配好的,所以我们只是计算就好了 ...

  9. quartz.net 学习

    目录 简介  Quartz是什么?  Quartz的应用场景Quartz的安装  安装  源码Hello World范例API  核心API    Scheduler接口:    Job接口    J ...

  10. 程序间获取ALV显示数据(读取ALV GRID上的数据)

    程序间获取ALV数据的两种方法: 方法1:通过修改SUBMIT的目标程序,把内表EXPORT到内存,SUBMIT后IMPORT ,该方法需要修改目标程序,可以任意设置目标程序的中断点: * Execu ...