我试图让工作的URL是以下风格的网址:http://somedomain.com/api/people/staff.33311(就像网站一样,LAST.FM允许在他们的RESTFul和WebPage网址中添加所有类型的标记例如," http://www.last.fm/artist/psy'aviah "是LAST.FM的有效网址.
以下方案有效: - http://somedomain.com/api/people/ - 返回所有人 - http://somedomain.com/api/people/staff33311 - 也可以,但不是我的意思在我希望网址接受"点"后,就像下面的示例 - http://somedomain.com/api/people/staff.33311 - 但这给了我一个
HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
Run Code Online (Sandbox Code Playgroud)
我已经设置了以下内容:
控制器"PeopleController"
public IEnumerable<Person> GetAllPeople()
{
return _people;
}
public IHttpActionResult GetPerson(string id)
{
var person = _people.FirstOrDefault(p => p.Id.ToLower().Equals(id.ToLower()));
if (person == null)
return NotFound();
return Ok(person);
}
Run Code Online (Sandbox Code Playgroud)WebApiConfig.cs
public static void Register(HttpConfiguration config)
{
// …Run Code Online (Sandbox Code Playgroud)c# asp.net-mvc-routing asp.net-mvc-4 asp.net-web-api asp.net-web-api2
我有一个ApiController,我想使用电子邮件地址作为请求的ID参数:
// GET api/employees/email@address.com
public CompactEmployee Get(string id) {
var email = id;
return GetEmployeeByEmail(email);
}
Run Code Online (Sandbox Code Playgroud)
但是,我不能让它工作(返回404):
http://localhost:1080/api/employees/employee@company.com
以下所有工作:
http://localhost:1080/api/employees/employee@companyhttp://localhost:1080/api/employees/employee@company.http://localhost:1080/api/employees?id=employee@company.com我已经设置relaxedUrlToFileSystemMapping="true"了我的web.config,详见Phil Haack.
我非常喜欢完整的电子邮件地址,但是任何时候任何其他角色跟着这个时期,请求都会返回404.任何帮助都将非常感谢!
由于缺乏其他选项,我已朝着Maggie建议的方向前进,并使用此问题的答案来创建重写规则,以便在我需要URL中的电子邮件时自动附加尾部斜杠.
<system.webServer>
....
<rewrite>
<rules>
<rule name="Add trailing slash" stopProcessing="true">
<match url="^(api/employees/.*\.[a-z]{2,4})$" />
<action type="Rewrite" url="{R:1}/" />
</rule>
</rules>
</rewrite>
</system.webServer>
Run Code Online (Sandbox Code Playgroud) 我希望你能帮我解决下面的问题.
我在IIS7上使用ASP.NET MVC 3,并希望我的应用程序支持带点的用户名.
这就是我的Global.asax的样子:(http:// localhost / {username})
routes.MapRoute(
"UserList",
"{username}",
new { controller = "Home", action = "ListAll" }
);
Run Code Online (Sandbox Code Playgroud)
当我访问其他页面时,应用程序工作,例如http://localhost/john.lee/details等.
但主用户页面不起作用,我希望该应用程序像支持http://www.facebook.com/john.lee的 Facebook一样工作.
我使用下面的代码,它根本不适合我:
<httpRuntime relaxedUrlToFileSystemMapping="true" />
Run Code Online (Sandbox Code Playgroud)
我能够使用下面的代码并让应用程序接受点,但我绝对不希望使用下面的代码有很多不同的原因,请告诉我有一种方法来克服这个问题.
<modules runAllManagedModulesForAllRequests="false" />
Run Code Online (Sandbox Code Playgroud) 我正在从原始的http处理程序移动API项目,我在路径中使用句点:
http://server/collection/id.format
Run Code Online (Sandbox Code Playgroud)
我想在Web Api(自托管)版本中遵循相同的URL架构,并尝试这样做:
var c = new HttpSelfHostConfiguration(b);
c.Routes.MapHttpRoute(
name: "DefaultApiRoute",
routeTemplate: "{controller}/{id}.{format}",
defaults: new { id = RouteParameter.Optional, format = RouteParameter.Optional },
constraints: null
);
Run Code Online (Sandbox Code Playgroud)
不幸的是,这似乎没有解决(在/ foo,/ foo/bar和/foo/bar.txt上一致的404').在'format'之前使用斜杠的类似模式工作正常:
var c = new HttpSelfHostConfiguration(b);
c.Routes.MapHttpRoute(
name: "DefaultApiRoute",
routeTemplate: "{controller}/{id}/{format}",
defaults: new { id = RouteParameter.Optional, format = RouteParameter.Optional },
constraints: null
);
Run Code Online (Sandbox Code Playgroud)
我还没有深入研究Web Api的代码,在此之前我想过我会在这里询问这是否是Web Api中已知的,甚至是合理的限制.
更新:我忽略了提到"id"和"format"是字符串,这对于解决这个问题很重要.添加约束以从"id"标记中排除句点可解决404问题.
我正在创建一个Web Api(v2.0)方法,需要将十进制值作为参数.
如果我使用以下URL,我收到404找不到错误:
http://localhost:4627/api/Product/Eligibility/10.5
Run Code Online (Sandbox Code Playgroud)
但是如果我对Int参数使用以下URL,它会起作用:
Http://localhost:4627/api/Product/Eligibility/10
Run Code Online (Sandbox Code Playgroud)
这些是api中的两个相应方法:
// GET api/Product/Eligibility/10.0
[Route("api/Product/Eligibility/{amount:decimal}")]
public decimal GetEligibiilty(decimal amount)
{
return amount;
}
// GET api/Product/Eligibility/10
[Route("api/Product/Eligibility/{amount:int}")]
public decimal GetEligibiilty(int amount)
{
return amount;
}
Run Code Online (Sandbox Code Playgroud)
史蒂夫
我需要编码一个包含点字符"."的网址.它是一个ASP.NET MVC路由,但url包含一个".".有办法吗?
例如,我正在尝试获取此URL:"/ Products/Beverages/Drink.Best/Teste"
有个 "." 在...那我需要编码...这可能吗?
谢谢!
这一般适用于ASP.NET,但也适用于Web API.
如何在不启用RAMMFAR(RunAllManagedModulesForAllRequests)的情况下处理PUT/DELETE谓词.
我无法在IIS中配置处理程序映射,因为我的站点托管在Azure Web角色上,并且我所做的任何更改都不会保留.
我似乎无法在asp.net MVC 5中使用自定义VirtualPathProvider.
FileExists方法返回true,但不调用GetFile方法.我相信这是因为IIS接管请求并且不让.NET处理它.
我已经尝试设置RAMMFAR并创建自定义处理程序,如此解决方案/sf/answers/850605101/但仍然没有运气.我收到错误404.
我的定制提供商:
public class DbPathProvider : VirtualPathProvider
{
public DbPathProvider() : base()
{
}
private static bool IsContentPath(string virtualPath)
{
var checkPath = VirtualPathUtility.ToAppRelative(virtualPath);
return checkPath.StartsWith("~/CMS/", StringComparison.InvariantCultureIgnoreCase);
}
public override bool FileExists(string virtualPath)
{
return IsContentPath(virtualPath) || base.FileExists(virtualPath);
}
public override VirtualFile GetFile(string virtualPath)
{
return IsContentPath(virtualPath) ? new DbVirtualFile(virtualPath) : base.GetFile(virtualPath);
}
public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
{
return null;
}
public override String GetFileHash(String virtualPath, IEnumerable virtualPathDependencies)
{ …Run Code Online (Sandbox Code Playgroud) 我有非常基本的路由:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",parameters
new { controller = "Home", action = "Index", id = "" }
);
Run Code Online (Sandbox Code Playgroud)
问题是它在所有简单的情况下都能很好地工作,例如www.xxx.yy/pages/filter/test".一旦我添加点"."到{id}部分它失败了,返回'无法找到资源'.它发生了www.xxx.yy/pages/filter/test.
有什么建议?
我正在开发一个网站,应该允许URL中的点,例如www.mysite.com/somedata/my.name-123.我希望的行为是使该URL等效www.mysite.com/somedata/myname-123,但我得到404错误,因为这些点使IIS尝试找到一个静态文件.类似的问题可以在这里找到,但是我提出的解决方案并不适用,我将在下面解释.这是我尝试过的:
添加旧问题中接受的答案中建议的新处理程序不起作用,因为我需要将其用于多个路径.如果我设置path ="*",那么所有静态文件(例如.css文件)都会失败.我可以使用RegEx指定一个url模式,但据我所知,path属性不允许它(只允许使用通配符)
使用<httpRuntime relaxedUrlToFileSystemMapping="true" />和<requestFiltering allowDoubleEscaping="true"/>web.config这样的行在MVC5中不起作用.
添加<modules runAllManagedModulesForAllRequests="true">到web.config中的system.webServer部分确实可以正常工作但是由于本文中公开的许多原因而不鼓励这种做法,所以我尽量避免使用这种方法.
在URL中附加一个斜杠(/)也可以解决问题,但这样做会导致Google认为我正在提供重复内容,因此我无法使用此解决方案.
我想不出任何其他选择.理想情况下,我想添加一个新的处理程序,使其适用于所有URL,但以某些模式(.css,.js)结尾的URL除外.
更新: 我找到了一个适合我的解决方案.我会尝试解释我采取的步骤:
首先,我找到了一个非常好的解释为什么会发生这种行为:Scott Forsyth的博客.还讨论了上面列出的一些解决方案.在对文章进行评论之后,我决定使用重写规则处理虚线网址.我决定在没有麻烦的点的情况下提供URL,然后检查用户在我的应用程序的控制器中查找的内容,而不是添加尾部斜杠.我的基本规则如下:
<rule name="remove troublesome dots" stopProcessing="false">
<match url="^(.*)\.(\S*)$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}{R:2}" />
</rule>
Run Code Online (Sandbox Code Playgroud)
请注意,每次删除一个点,因此具有多个点的URL会多次触及此规则.如果网址可能包含大量点,则这很糟糕.
如果静态文件路径没有直接映射它的磁盘位置,我的方法将使它崩溃.为了避免出现问题,我们需要为我们需要的文件结尾添加删除点规则的例外.Seb Nilsson 在本博文中解释了如何在"为特定文件结尾添加条件"部分下执行此操作.
asp.net-mvc ×5
c# ×5
asp.net ×3
iis ×2
url-routing ×2
global-asax ×1
iis-7 ×1
regex ×1
routing ×1
urlencode ×1