本文整理汇总了C#中System.Web.Http.SelfHost.HttpSelfHostConfiguration.MapHttpAttributeRoutes方法的典型用法代码示例。如果您正苦于以下问题:C# HttpSelfHostConfiguration.MapHttpAttributeRoutes方法的具体用法?C# HttpSelfHostConfiguration.MapHttpAttributeRoutes怎么用?C# HttpSelfHostConfiguration.MapHttpAttributeRoutes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Web.Http.SelfHost.HttpSelfHostConfiguration
的用法示例。
在下文中一共展示了HttpSelfHostConfiguration.MapHttpAttributeRoutes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
public static void Main(string[] args)
{
var config = new HttpSelfHostConfiguration("http://127.0.0.1:3001");
config.MapHttpAttributeRoutes();
config.EnableCors();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ReportsApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
using (HttpSelfHostServer server = new HttpSelfHostServer(config))
{
server.OpenAsync().Wait();
Console.WriteLine("Press Enter to quit.");
Console.ReadLine();
}
}
示例2: PrinterService
public PrinterService()
{
InitializeComponent();
config = new HttpSelfHostConfiguration(AppConfig.BaseUrl);
config.MapHttpAttributeRoutes();
}
示例3: Main
/// <summary>
/// The program main method
/// </summary>
/// <param name="args">The program arguments</param>
private static void Main(string[] args)
{
var config = new HttpSelfHostConfiguration(ServiceAddress);
config.MapHttpAttributeRoutes();
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
config.EnableCors(new EnableCorsAttribute("*", "*", "*"));
using (var server = new HttpSelfHostServer(config)) {
try {
server.OpenAsync().Wait();
Console.WriteLine("Service running on " + ServiceAddress + ". Press enter to quit.");
Console.ReadLine();
}
catch (Exception e) {
if (!IsAdministrator()) {
Console.WriteLine("Please restart as admin.");
Debug.WriteLine("Restart Visual Studio as admin");
}
else {
Console.WriteLine("Server failed to start.");
}
Console.ReadLine();
}
}
}
示例4: Main
static void Main(string[] args)
{
var address = "http://localhost:900";
var config = new HttpSelfHostConfiguration(address);
config.MapHttpAttributeRoutes();
using (var server = new HttpSelfHostServer(config))
{
server.OpenAsync().Wait();
Console.WriteLine("Server running at {0}. Press any key to exit", address);
var client = new HttpClient() {BaseAddress = new Uri(address)};
var persons = client.GetAsync("person").Result;
Console.WriteLine(persons.Content.ReadAsStringAsync().Result);
var newPerson = new Person {Id = 3, Name = "Luiz"};
var response = client.PostAsJsonAsync("person", newPerson).Result;
if (response.IsSuccessStatusCode)
{
var person3 = client.GetAsync("person/3").Result;
Console.WriteLine(person3.Content.ReadAsStringAsync().Result);
}
Console.ReadLine();
}
}
示例5: Main
static void Main(string[] args)
{
HttpSelfHostConfiguration configuration = new HttpSelfHostConfiguration("http://localhost:3721");
configuration.MapHttpAttributeRoutes();
HttpSelfHostServer server = new HttpSelfHostServer(configuration);
server.OpenAsync().Wait();
Console.Read();
}
示例6: Main
private static void Main()
{
const string address = "http://localhost:925/";
var config = new HttpSelfHostConfiguration(address);
config.MapHttpAttributeRoutes();
var handler = new HawkMessageHandler(
async id => new HawkCredential
{
Id = id,
Key = "abcdefghijkl",
Algorithm = "sha256",
User = "filip"
}, 4, true);
config.MessageHandlers.Add(handler);
using (var server = new HttpSelfHostServer(config))
{
server.OpenAsync().Wait();
var client = new HttpClient();
//this will fail
var request = new HttpRequestMessage(HttpMethod.Get, address + "test");
var response = client.SendAsync(request).Result;
Console.WriteLine(response.StatusCode);
Console.WriteLine();
var credential = new HawkCredential
{
Id = "this-is-my-id",
Key = "abcdefghijkl",
Algorithm = "sha256",
User = "filip"
};
var clientHandler = new HawkClientMessageHandler(new HttpClientHandler(), credential, ts: DateTime.Now);
var client2 = new HttpClient(clientHandler);
//this will succeed
request = new HttpRequestMessage(HttpMethod.Get, address + "test");
var response2 = client2.SendAsync(request).Result;
Console.WriteLine(response2.StatusCode);
Console.WriteLine(response2.Content.ReadAsStringAsync().Result);
Console.WriteLine();
Console.WriteLine("Sleeping to get outside of the timestamp window. Next request will fail - replay protection.");
Thread.Sleep(5000);
//this will fail
request = new HttpRequestMessage(HttpMethod.Get, address + "test");
var response3 = client2.SendAsync(request).Result;
Console.WriteLine(response3.StatusCode);
Console.WriteLine();
Console.ReadLine();
}
}
示例7: MediaStreamingServer
public MediaStreamingServer(int port = 8080)
{
Address = "http://" + Ext.GetIp4Address() + ":" + port;
var config = new HttpSelfHostConfiguration(Address);
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new {id = RouteParameter.Optional}
);
config.TransferMode = TransferMode.Streamed;
httpSelfHostServer = new HttpSelfHostServer(config);
}
示例8: OnStart
protected override void OnStart(string[] args)
{
var selfHostConfiguraiton = new HttpSelfHostConfiguration("http://localhost:9999/");
selfHostConfiguraiton.Routes.MapHttpRoute(
name: "ApiRoute",
routeTemplate: "api/{controller}",
defaults: null
);
selfHostConfiguraiton.MapHttpAttributeRoutes();
selfHostConfiguraiton.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
_webApiServer = new HttpSelfHostServer(selfHostConfiguraiton);
_webApiServer.OpenAsync().Wait();
}
示例9: Main
static void Main(string[] args)
{
var baseAddress = "http://localhost:8080";
HttpSelfHostConfiguration config = new HttpSelfHostConfiguration(baseAddress);
// config.MessageHandlers.Add(new ProgressMessageHandler() { });
// Web API 路由
config.MapHttpAttributeRoutes();
//全局允许CROS
// config.EnableCors();//启用跨域
config.Routes.MapHttpRoute
(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.MaxConcurrentRequests = 1000;
var handlers = new DelegatingHandler[] { new PassiveAuthenticationMessageHandler(), new HttpServer() };
var routeHandlers = HttpClientFactory.CreatePipeline(new HttpControllerDispatcher(config), handlers);
config.Routes.MapHttpRoute(
name: "CustomerRouter",
routeTemplate: "MyAPI/{Controller}/{Action}/Id",
defaults: new { Id = RouteParameter.Optional },
constraints: null,
handler: routeHandlers
);
HttpSelfHostServer server = new HttpSelfHostServer(config);
server.OpenAsync();
TaskManager.Instance.Initialize();
TaskManager.Instance.Start();
DefaultHttpBatchHandler batchHandler = new DefaultHttpBatchHandler(server);
Console.WriteLine("Server http://localhost:8080 Open now ....at {0}..", server.Configuration.VirtualPathRoot);
config.EnsureInitialized();
foreach (var route in config.Routes)
{
System.Diagnostics.Debug.WriteLine(route);
}
Console.ReadLine();
}
示例10: Service
public Service()
{
var selfHostConfiguraiton = new HttpSelfHostConfiguration("http://localhost:5555");
selfHostConfiguraiton.MapHttpAttributeRoutes();
selfHostConfiguraiton.MapODataServiceRoute("api", "api", GetApiModel());
selfHostConfiguraiton.Routes.MapHttpRoute(
name: "DefaultApiRoute",
routeTemplate: "{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
server = new HttpSelfHostServer(selfHostConfiguraiton);
}
示例11: SetUp
void SetUp()
{
Injector.Resolver = new TestResolver();
var endpoint = "http://localhost:40000";
target = new RemoteDecisionService(endpoint);
var config = new HttpSelfHostConfiguration(endpoint);
config.DependencyResolver = new InjectorDependencyResolver();
// Decision routes
config.MapHttpAttributeRoutes();
server = new HttpSelfHostServer(config);
server.OpenAsync().Wait();
}
示例12: Start
public Uri Start(string serviceName, string version)
{
var uri = Configuration.GetUri();
var config = new HttpSelfHostConfiguration(uri);
// Attribute routing.
config.MapHttpAttributeRoutes();
// Convention-based routing.
config.Routes.MapHttpRoute(
"API Default", "{controller}/{id}",
new { id = RouteParameter.Optional });
var server = new HttpSelfHostServer(config);
server.OpenAsync().Wait();
return uri;
}
示例13: Start
public void Start()
{
var nodeConfigString = File.ReadAllText("config.json");
var settings = new Newtonsoft.Json.JsonSerializerSettings
{
TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Auto,
};
var config = Newtonsoft.Json.JsonConvert.DeserializeObject<JSONConfig>(nodeConfigString, settings);
// config Syslog listener
if (config.syslog.listenerEnabled)
{
var listener = new SyslogListener();
Action<int> startListener = listener.Start;
startListener.BeginInvoke(config.syslog.listenerPort, null, null);
}
// config PollEngine
foreach (var node in (config.nodes ?? new JSONConfigNode[0]))
{
var pollNode = Activator.CreateInstance(typeof(PollingEngine).Assembly.GetType(node.type), new object[] { node.name, node.groups ?? new string[0], node.settings }) as PollNode;
PollingEngine.TryAdd(pollNode);
}
PollingEngine.StartPolling();
// config HTTP API
var hostConfig = new HttpSelfHostConfiguration("http://127.0.0.1:" + config.http.listenerPort);
// Remove the XML formatter
hostConfig.Formatters.Remove(hostConfig.Formatters.XmlFormatter);
hostConfig.Formatters.Add(new RazorFormatter());
hostConfig.Formatters.JsonFormatter.SerializerSettings.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Auto;
hostConfig.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
hostConfig.Formatters.JsonFormatter.SerializerSettings.Error += (x, y) =>
{
return;
};
hostConfig.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("type", "json", new MediaTypeHeaderValue("application/json")));
// Attribute routing.
hostConfig.MapHttpAttributeRoutes();
hostConfig.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
hostConfig.Filters.Add(new ApiExceptionFilterAttribute());
httpServer = new HttpSelfHostServer(hostConfig);
httpServer.OpenAsync().Wait();
}
示例14: Main
static void Main(string[] args)
{
var config = new HttpSelfHostConfiguration("http://localhost:999");
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var server = new HttpSelfHostServer(config);
config.CacheOutputConfiguration().RegisterCacheOutputProvider(() => new MemoryCacheDefault());
server.OpenAsync().Wait();
Console.ReadKey();
server.CloseAsync().Wait();
}
示例15: Service
public Service()
{
var selfHostConfiguraiton = new HttpSelfHostConfiguration("http://localhost:5555");
selfHostConfiguraiton.MapHttpAttributeRoutes();
selfHostConfiguraiton.Routes.MapHttpRoute(
name: "DefaultApiRoute",
routeTemplate: "api/{controller}",
defaults: null
);
selfHostConfiguraiton.Routes.MapHttpRoute(
name: "ApiRouteWithAction",
routeTemplate: "api/{controller}/{action}",
defaults: null
);
server = new HttpSelfHostServer(selfHostConfiguraiton);
}