可以在时间Asp.Net网页API路由使用? [英] Can periods be used in Asp.Net Web Api Routes?
问题描述
我的工作正从原材料HTTP处理程序在那里我使用的路径时期的API项目:
I'm working on moving an API project from raw http handlers where I'm using periods in the paths:
http://server/collection/id.format
我想按照一个网页API相同的URL模式(自托管)版本,并尝试这样的:
I would like to follow the same URL schema in a Web Api (self-hosted) version, and tried this:
var c = new HttpSelfHostConfiguration(b);
c.Routes.MapHttpRoute(
name: "DefaultApiRoute",
routeTemplate: "{controller}/{id}.{format}",
defaults: new { id = RouteParameter.Optional, format = RouteParameter.Optional },
constraints: null
);
不幸的是,这似乎并没有解决(上/ foo的404一致的,/富/酒吧和/foo/bar.txt)。使用格式前斜线类似的模式正常工作:
Unfortunately, that doesn't seem to resolve (consistent 404's on /foo, /foo/bar and /foo/bar.txt). A similar pattern using a slash before 'format' works fine:
var c = new HttpSelfHostConfiguration(b);
c.Routes.MapHttpRoute(
name: "DefaultApiRoute",
routeTemplate: "{controller}/{id}/{format}",
defaults: new { id = RouteParameter.Optional, format = RouteParameter.Optional },
constraints: null
);
我还没有钻研code为Web API和我做思想之前,我会问这里看到,如果这是一个已知的,还是在网页API甚至也许是合法的限制。
I haven't yet delved into the code for the Web Api, and before I do thought I'd ask here to see if this is a known, or perhaps even justified limitation in Web Api.
更新:我忘了提,ID和格式化是字符串,这原来是解决这一问题的重要。添加约束从ID令牌排除期间解决404问题。
UPDATE: I neglected to mention that "id" and "format" are strings, which turns out to be important for the solution to this question. Adding a constraint to exclude periods from the "id" token solves the 404 problem.
推荐答案
我无法重现该问题。这应该工作。这里是我的设置:
I am unable to reproduce the problem. This should work. Here's my setup:
- 创建一个新的.NET 4.0的控制台应用程序
- 切换到.NET Framework 4.0配置文件
- 安装
Microsoft.AspNet.WebApi.SelfHost
的NuGet -
定义
产品
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
}
一个相应的API控制器:
A corresponding API Controller:
public class ProductsController : ApiController
{
public Product Get(int id)
{
return new Product
{
Id = id,
Name = "prd " + id
};
}
}
和主机:
And a host:
class Program
{
static void Main(string[] args)
{
var config = new HttpSelfHostConfiguration("http://localhost:8080");
config.Routes.MapHttpRoute(
name: "DefaultApiRoute",
routeTemplate: "{controller}/{id}.{format}",
defaults: new { id = RouteParameter.Optional, format = RouteParameter.Optional },
constraints: null
);
using (var server = new HttpSelfHostServer(config))
{
server.OpenAsync().Wait();
Console.WriteLine("Press Enter to quit.");
Console.ReadLine();
}
}
}
现在,当您运行此控制台应用程序,你可以浏览到的http://本地主机:8080 /产品/ 123.xml
。当然,你可以浏览到的http://本地主机:8080 /产品/ 123.json
,你仍然会得到XML。所以,问题是:如何使用路由参数,使内容协商
Now when you run this console application you could navigate to http://localhost:8080/products/123.xml
. But of course you could navigate to http://localhost:8080/products/123.json
and you will still get XML. So the question is: How to enable content negotiation using a route parameter?
您可以做到以下几点:
class Program
{
static void Main(string[] args)
{
var config = new HttpSelfHostConfiguration("http://localhost:8080");
config.Formatters.XmlFormatter.AddUriPathExtensionMapping("xml", "text/html");
config.Formatters.JsonFormatter.AddUriPathExtensionMapping("json", "application/json");
config.Routes.MapHttpRoute(
name: "DefaultApiRoute",
routeTemplate: "{controller}/{id}.{ext}",
defaults: new { id = RouteParameter.Optional, formatter = RouteParameter.Optional },
constraints: null
);
using (var server = new HttpSelfHostServer(config))
{
server.OpenAsync().Wait();
Console.WriteLine("Press Enter to quit.");
Console.ReadLine();
}
}
}
,现在你可以使用以下网址:
and now you can use the following urls:
http://localhost:8080/products/123.xml
http://localhost:8080/products/123.json
现在,你可能想知道什么是 {转}
,我们在我们的路线定义和使用路径参数 AddUriPathExtensionMapping $之间的关系C $ C>方法,因为无处我们没有指定。那么,猜猜是什么:它在
UriPathExtensionMapping
类硬coded到外部
,你不能修改它,因为它是只读的:
Now you might be wondering what's the relation between the {ext}
route parameter that we used in our route definition and the AddUriPathExtensionMapping
method because nowhere we did not specify it. Well, guess what: it's hardcoded in the UriPathExtensionMapping
class to ext
and you cannot modify it because it is readonly:
public class UriPathExtensionMapping
{
public static readonly string UriPathExtensionKey;
static UriPathExtensionMapping()
{
UriPathExtensionKey = "ext";
}
...
}
这一切来回答你的问题:
All this to answer your question:
能否时期在Asp.Net网页API路线使用吗?
Can periods be used in Asp.Net Web Api Routes?
是
这篇关于可以在时间Asp.Net网页API路由使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!