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 ...
随机推荐
- 牛顿法与拟牛顿法(三) DFP算法
转自 https://blog.csdn.net/itplus/article/details/21896981
- 在基于Android以及Jetson TK平台上如何写32位的Thumb-2指令
由于Android以及Jetson TK的编译工具链中的汇编器仍然不支持大部分的32位Thumb-2指令,比如add.w,因此我们只能通过手工写机器指令码来实现想要的指令.下面我将简单地介绍如何在AR ...
- Onvif协议及其在Android下的实现
好久没有写博客,今天将前段时间做的Onvif协议在Android上的实现分享给大家. 首先,我们先来了解一下什么是Onvif协议:ONVIF 协议是由Open Network Video Interf ...
- java.lang.NoClassDefFoundError: org/springframework/aop/TargetSource
在使用Spring框架时 报错 :java.lang.NoClassDefFoundError: org/springframework/aop/TargetSource 原因:为引入spring-a ...
- Python字符串逐字符或逐词反转方法
Python字符串逐字符或逐词反转方法 这篇文章主要介绍了Python字符串逐字符或逐词反转方法,本文对逐字符或逐词分别给出两种方法,需要的朋友可以参考下 目的 把字符串逐字符或逐词反转过来,这个蛮有 ...
- JQ也要面向对象~在JQ中扩展静态方法和实例方法(jq扩展方法)
JQ也要面向对象,事实上,无论哪种开发语言,在开发功能时,都要把面向对象拿出来,用它的思想去干事,去理解事,面向对象会使问题简单化,清晰化,今天说两个概念“静态方法”与“实现方法”,这个在面向对象的语 ...
- mysql学习笔记11_12(查询)
1.建表和插入值 创建company数据库 创建 department表 create table department(d_id int(10) primary key not null uniqu ...
- centos(linux)-jdk配置
1.清理系统默认自带的jdk 在安装centos时,可能系统会默认安装了例如openjdk等,需要先手动卸载 先执行:rpm -qa | grep jdk (查看已经自带的jdk): 卸载命名:sud ...
- Python学习笔记——GUI
1. 相关文档 EasyGui 官网:http://easygui.sourceforge.net 官方的教学文档:easygui-docs-0.96\tutorial\index.html 小甲鱼翻 ...
- Vue CLI 3 如何自定义 js 的文件名
参考链接:https://blog.csdn.net/weixin_33979363/article/details/88742342