c# ServiceStack web 搭建

用的是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 搭建的更多相关文章
- ServiceStack Web Service 创建与调用简单示列
目录 ServiceStack 概念 ServiceStack Web Service 创建与调用简单示列 上篇文章介绍了ServiceStack是什么,本章进入主题,如何快速简单的搭建Service ...
- IIS web搭建之虚拟主机
IIS web搭建之虚拟主机 虚拟目录:能将一个网站的文件分散存储在同一个计算机的不同目录和其他计算机. 使用虚拟目录的好处: 1.将数据分散保存到不同的磁盘或者计算机上,便于分别开发和维护. 2.当 ...
- Shiro的原理及Web搭建
shiro(java安全框架) 以下都是综合之前的人加上自己的一些小总结 Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码学和会话管理.使用Shiro的易于理解的A ...
- Tomcat WEB搭建+Nginx负载均衡动静分离+DNS解析的实验
实验拓扑图: 实验环境: 在VMware workstation搭建虚拟环境,利用网络适配器的Nat和桥接模式模拟内网和外网环境. 实验过程中需要安装的工具包包括:vim unzip lrzsz ls ...
- Flask 使用pycharm 创建项目,一个简单的web 搭建
1:新建项目后 2:Flask web 项目重要的就是app 所有每个都需要app app=Flask(__name__) 3:Flask 的路径是有app.route('path')装饰决定, ...
- pycharm环境下用Python+Django开发web搭建
1.安装pycharm: 2.安装Python: 3.安装mysql: 4.安装Django; pip3 install django 5.创建Django工程命令方式: # 创建Django程序 d ...
- 使用 ServiceStack 构建跨平台 Web 服务
本文主要来自MSDN杂志<Building Cross-Platform Web Services with ServiceStack>,Windows Communication Fou ...
- 使用 ServiceStack 构建跨平台 Web 服务(转)
出处:http://www.cnblogs.com/shanyou/p/3348347.html 本文主要来自MSDN杂志<Building Cross-Platform Web Service ...
- Win7下Python WEB环境搭建
环境介绍: Win7 64位 SP1 Python:2.7.6 网关接口:flup Nginx安装:http://blog.csdn.net/jacson_bai/article/details/46 ...
随机推荐
- 一个U盘制作多个系统
写在前面:一个U盘可以装多个ghost系统,但不能装多个原版ISO系统. 一.一个U盘装多个ghost系统 下载老毛桃或电脑店U盘制作工具,点击一键制作U盘启动盘.然后将gho文件拷贝复制到U盘的GH ...
- pre-fork 分叉 软分叉 硬分叉 前叉实现 pre-fork implementation
https://mp.weixin.qq.com/s/wIDTs2J1ZkLkAEHqQnkYnw 什么是分叉?为何对区块链发展至关重要? Uselink公有链 Uselink公有链 2018-12- ...
- kotlin中匿名对象
open class MyClass { private fun too()=object { var x : String ="x" } fun publictoo()=obje ...
- C之交换数据案例
//值传递 void swap(int i,int j){ printf("交换后:\n "); int tmp; tmp = i; i = j; j = tmp; } //引用传 ...
- smarty 对数据库的简单操作。
smarty简单配置 -------------------------------------smarty_inc.php-------------------------------------- ...
- pip使用笔记
例子: pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -U funcat -i: 指定库的安装源 -U:升级 原来已经安装的包,不带U ...
- spring常用模式--委派模式
1.委派模式简介 在常用的23种设计模式中其实面没有委派模式(delegate)的影子,但是在Spring中委派模式确实用的比较多的一种模式. 在spring中的体现:Spring MVC框架中的Di ...
- Tomcat 80端口被占用
1.“运行”中输入cmd2.在命令行中输入netstat -ano,得到端口号对应的PID 3.打开任务管理器,点击“查看“菜单,选择“选择列”,给进程列表中添加”PID“列,然后找到PID对应的进程 ...
- bash小结
context:CentOS 什么是shell? shell就是与计算机交互的接口. linux支持的shell [root@node1 ~]# cat /etc/shells /bin/sh #被 ...
- 常见MIME类型列表整理
译者注:英文原文标题为 Incomplete list of MIME types,意为不完整的/未完成的 MIME 类型列表. 这是一份 MIME 类型列表,以及各个类型的文档类别,按照它们的常见扩 ...