Routing Tables

In ASP.NET Web API, a controller is a class that handles HTTP requests. The public methods of the controller are called action methods or simply actions. When the Web API framework receives a request, it routes the request to an action.

To determine which action to invoke, the framework uses a routing table. The Visual Studio project template for Web API creates a default route:

routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);

Self-Host a Web API (C#)

/// <summary>
/// 根据键值获取配置文件
/// </summary>
/// <param name="key">键值</param>
/// <param name="defaultValue">默认值</param>
/// <returns></returns>
public static string GetConfig(string key, string defaultValue)
{
string val = defaultValue;
if (ConfigurationManager.AppSettings.AllKeys.Contains(key))
val = ConfigurationManager.AppSettings[key];
if (val == null)
val = defaultValue;
return val;
} /// <summary>
/// 写配置文件,如果节点不存在则自动创建
/// </summary>
/// <param name="key">键值</param>
/// <param name="value">值</param>
/// <returns></returns>
public static bool SetConfig(string key, string value)
{
try
{
Configuration conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (!conf.AppSettings.Settings.AllKeys.Contains(key))
conf.AppSettings.Settings.Add(key, value);
else
conf.AppSettings.Settings[key].Value = value; ;
conf.Save();
return true;
}
catch(Exception e)
{
return false;
}
} /// <summary>
/// 写配置文件(用键值创建),如果节点不存在则自动创建
/// </summary>
/// <param name="dict">键值集合</param>
/// <returns></returns>
public static bool SetConfig(Dictionary<string, string> dict)
{
try
{
if (dict == null || dict.Count == )
return false;
Configuration conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
foreach (string key in dict.Keys)
{
if (!conf.AppSettings.Settings.AllKeys.Contains(key))
conf.AppSettings.Settings.Add(key, dict[key]);
else
conf.AppSettings.Settings[key].Value = dict[key];
}
conf.Save();
return true;
}
catch { return false; }
}
}

Routing in ASP.NET Web API和配置文件的设定读取的更多相关文章

  1. Routing in ASP.NET Web API

    Why is HttpGet required only for some actions? https://stackoverflow.com/questions/28068868/why-is-h ...

  2. web api :Routing in ASP.NET Web API

    引 Web API 和SignalR都是在服务层. If you are familiar with ASP.NET MVC, Web API routing is very similar to M ...

  3. Create a REST API with Attribute Routing in ASP.NET Web API 2

    原文:http://www.asp.net/web-api/overview/web-api-routing-and-actions/create-a-rest-api-with-attribute- ...

  4. ASP.NET Web API中的Routing(路由)

    [译]Routing in ASP.NET Web API 单击此处查看原文 本文阐述了ASP.NET Web API是如何将HTTP requests路由到controllers的. 如果你对ASP ...

  5. 【ASP.NET Web API教程】4.1 ASP.NET Web API中的路由

    原文:[ASP.NET Web API教程]4.1 ASP.NET Web API中的路由 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的内容. ...

  6. ASP.NET Web Api

    1.参考资料 Routing in Asp.NET Web Api: http://www.asp.net/web-api/overview/web-api-routing-and-actions/r ...

  7. Getting Started with ASP.NET Web API 2 (C#)

    By Mike Wasson|last updated May 28, 2015 7556 of 8454 people found this helpful Print   Download Com ...

  8. ASP.NET Web API系列教程目录

    ASP.NET Web API系列教程目录 Introduction:What's This New Web API?引子:新的Web API是什么? Chapter 1: Getting Start ...

  9. 【ASP.NET Web API教程】1.1 第一个ASP.NET Web API

    Your First ASP.NET Web API (C#)第一个ASP.NET Web API(C#) By Mike Wasson|January 21, 2012作者:Mike Wasson ...

随机推荐

  1. php Hash Table(四) Hash Table添加和更新元素

    HashTable添加和更新的函数: 有4个主要的函数用于插入和更新HashTable的数据: int zend_hash_add(HashTable *ht, char *arKey, uint n ...

  2. python中单引号,双引号,多引号区别

    先说1双引号与3个双引号的区别,双引号所表示的字符串通常要写成一行如:s1 = "hello,world"如果要写成多行,那么就要使用\ (“连行符”)吧,如s2 = " ...

  3. [webgrid] – header - (How to Add custom html to Header in WebGrid)

    How to Add custom html to Header in WebGrid MyEvernote Link Posted on March 30, 2013by mtryambake Ho ...

  4. cookie和session的对比

    1.存放的位置     cookie存在客户端的临时文件夹     session:存在服务器的内存中,一个session域对象为一个用户浏览器服务. 2.安全性   cookie是以明文方式存放在客 ...

  5. GC垃圾回收机制

    学习资料:http://kb.cnblogs.com/page/106720/

  6. ng指令之 ng-repeat 篇

    1>数据绑定     ng-repeat可以绑定数组和JSON对象数据.从下图可以看出控制器的scope()函数得到的对象与controller('ctrlName',['$scope',fun ...

  7. jquery 清除动画队列不疑惑

    $(this).siblings().stop().fadeTo(200, 0.3); jquery动画存在一个队列, 会把事件产生的动画 放在一个队列中,当来不及执行这些事件队列的时候,会在事件结束 ...

  8. 正确使用Python logging

    这篇文章主要参考: http://victorlin.me/posts/2012/08/26/good-logging-practice-in-python ===================== ...

  9. [设计模式] Javascript 之 观察者模式

    观察者模式:定议 定义对象间的一种一对多的关系,当一个对象状态改变时 (一般称为被观察者),依赖于该对象的对象被通知,并更新; 观察者模式:说明 1. 观察者模式是行为模式,也被称为:发布-订阅模式. ...

  10. isNaN() 确认是否是数字

    isNaN(x): 当变量 x 不是数字,返回 true: 当变量 x 是其他值,(比如,1,2,3),返回false.