相关疑难解决方法(0)

点字符'.' 在MVC Web API 2中请求诸如api/people/STAFF.45287之类的请求

我试图让工作的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)

我已经设置了以下内容:

  1. 控制器"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)
  2. 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

102
推荐指数
5
解决办法
5万
查看次数

带有一个必需参数和一个可选参数的ASP.NET MVC路由?

在过去一个月左右的时间里,我一直在研究一个大型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

13
推荐指数
1
解决办法
2万
查看次数