我试图让工作的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
在过去一个月左右的时间里,我一直在研究一个大型MVC应用程序,但这是我第一次需要定义一个自定义路由处理程序,而且我遇到了一些问题.基本上我有两个参数要通过.第一个是必需的,第二个是可选的.
我在这里听到这个答案.
这是我的自定义路线:
routes.MapRoute(
"MyRoute",
"{controller}/{action}/{param1}/{param2}",
new {
controller = "MyController",
action = "MyAction",
param1 = "",
param2 = "" // I have also tried "UrlParameter.Optional" here.
}
);
Run Code Online (Sandbox Code Playgroud)
我的动作方法签名:
public ActionResult MyAction(string param1, string param2)
Run Code Online (Sandbox Code Playgroud)
如果我尝试使用URL,http://[myserver]/MyController/MyAction/Test1/Test2
那么它就像我期望的那样工作,param1 ="Test1"和param2 ="Test2"
如果我尝试URL,http://[myserver]/MyController/MyAction/Test1
则两个参数都为空.
希望有人可以告诉我这里我做错了什么,因为我迷路了.
c# asp.net-mvc url-routing asp.net-mvc-routing asp.net-mvc-3