用的是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. brew 切换国内的源

    切换到国内源 # 替换brew.git: cd "$(brew --repo)" # 中国科大: git remote set-url origin https://mirrors ...

  2. mysql用户与授权

    视图 create view 视图名   as  查询语句: MariaDB [hellodb]> create view view_left as select name from stude ...

  3. 阶段5 3.微服务项目【学成在线】_day04 页面静态化_16-页面静态化-模板管理-模板制作

    这是轮播图的原始文件 运行门户需要把 nginx启动起来 单独运行轮播图.把里面的css的引用都加上网址的url 这就是单独访问到的轮播图的效果 轮播图模板的地址: 阶段5 3.微服务项目[学成在线] ...

  4. 阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_18-异常处理-不可预知异常处理

    框架抛出来的或者一些第三方的组件抛出来的异常.我们根本不知道它所对应的错误代码的信息,所以我们也没有办法给用户返回具体的错误代码和错误信息. 我们先在Map中定义有一些不可预知的异常,定义错误代码和错 ...

  5. python3 高级编程(三) 使用@property

    @property装饰器就是负责把一个方法变成属性调用的. @property广泛应用在类的定义中,可以让调用者写出简短的代码,同时保证对参数进行必要的检查,这样,程序运行时就减少了出错的可能性 cl ...

  6. zip炸弹

    故障系统有人提了zip炸弹的故障,了解了一些关于zip炸弹的常识. 42.zip 是很有名的zip炸弹.一个42KB的文件,解压完其实是个4.5PB的“炸弹”. 更有甚者,一个叫做 droste.zi ...

  7. CockroachDB学习笔记——[译]为什么Go语言是CockroachDB的正确选择

    原文链接:https://www.cockroachlabs.com/blog/why-go-was-the-right-choice-for-cockroachdb/ 原作者:Jessica Edw ...

  8. Hadoop 部署之 ZooKeeper (二)

    目录 一.Zookeeper功能简介 二.ZooKeeper基本概念 1.集群角色 三.ZooKeeper 的安装 1.下载安装(在datanode节点安装) 2.配置ZooKeeper环境变量 3. ...

  9. SSH命令工具研究报告

    0 什么是SSH Secure Shell(安全外壳协议,简称SSH)是一种加密的网络传输协议,可在不安全的网络中为网络服务提供安全的传输环境.SSH通过在网络中创建安全隧道来实现SSH客户端与服务器 ...

  10. linux基本防护措施,权限分配和提高防护安全

    设置用户失效 1.失效的用户将无法登录 使用chage命令将用户zhangsan的账户设为当前已失效(比如已经过去的某个时间): [root@proxy ~]# useradd zhangsan [r ...