简体   繁体   English

找不到与请求URI'MyUrl'匹配的HTTP资源

[英]No HTTP resource was found that matches the request URI 'MyUrl'

I created a .Net web service: 我创建了一个.Net Web服务:

public class UsersController : ApiController
{
    [System.Web.Http.HttpPost]
    public void Post(string value)
    {
        SqlConnection connection = new SqlConnection("Data Source=198.71.226.6;Integrated Security=False;User ID=AtallahMaroniteDB;Password=a!m?P@$$123;Database=AtallahPlesk_;Connect Timeout=15;Encrypt=False;Packet Size=4096");


        String query = "INSERT INTO Members(LastName, FirstName, Gender, MobileNumber, EmailAddress, Job, Address) VALUES " +
            "(@LastName, @FirstName, @Gender, @MobileNumber, @EmailAddress, @Job, @Address)";
        SqlCommand command = new SqlCommand(query, connection);
        try
        {
            JavaScriptSerializer json_serializer = new JavaScriptSerializer();
            PersonModel person = json_serializer.Deserialize<PersonModel>(value);

            command.Parameters.Add("@LastName", person.LastName);
            command.Parameters.Add("@FirstName", person.FirstName);
            command.Parameters.Add("@Gender", person.Gender);
            command.Parameters.Add("@MobileNumber", person.MobileNumber);
            command.Parameters.Add("@EmailAddress", person.EmailAddress);
            command.Parameters.Add("@Job", person.Job);
            command.Parameters.Add("@Address", person.Address);

            connection.Open();
            command.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.InnerException.ToString());
        }
    }
}

And the following is my routing config: 以下是我的路由配置:

RouteTable.Routes.MapHttpRoute(
     name: "MyApi",
     routeTemplate: "api/{controller}/{action}/{value}"
 );

When I am calling this web service from an Android or iOS application, I am getting the following error: 当我从Android或iOS应用程序调用此Web服务时,出现以下错误:

No HTTP resource was found that matches the request URI ' http://www.mytestdomain.com/api/users/post ' 没有HTTP资源发现,请求URI“匹配http://www.mytestdomain.com/api/users/post

Below is the android code: 以下是android代码:

JSONObject dato = POST(person); // This method converts the Person object to JSONObject

String text = null;
try {
    HttpPost post = new HttpPost("http://www.mytestdomain.com/api/users/post");
    StringEntity entity = new StringEntity(dato.toString());
    entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
    post.setEntity(entity);
    HttpResponse response = httpClient.execute(post);
    HttpEntity entityResponse = response.getEntity();
    text = getASCIIContentFromEntity(entityResponse);                   
} catch ( IOException ioe ) {
    ioe.printStackTrace();
}

Please note that when I call this web service from postman, it's posting the data successfully. 请注意,当我从邮递员那里调用此Web服务时,它成功地发布了数据。

Please let me know if you need any further details. 如果您需要更多详细信息,请告诉我。

You need to update your route template to make sure that you get a valid match for your request. 您需要更新您的路线模板,以确保您的请求得到有效的匹配。

Here is what a valid template would look like for your API. 这是您的API的有效模板。 Note this is specific to the UsersController as the defaults: has been set to controller = "Users" which will map to the UsersController 请注意,这是defaults:情况下特定于UsersControllerdefaults:已设置为controller = "Users" ,它将映射到UsersController

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Attribute routing.
        config.MapHttpAttributeRoutes();

        // Convention-based routing.

        config.Routes.MapHttpRoute(
            name: "MyApi",
            routeTemplate: "api/users/{action}",
            defaults: new { controller = "Users" }
        );

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

From your code example you are sending the model as json and then trying to manually parse it on the server. 从您的代码示例中,您将模型作为json发送,然后尝试在服务器上手动解析它。 You can let the framework parse the model with its model binders based on the Content-Type of the request. 您可以让框架根据请求的Content-Type解析带有模型绑定器的模型。 This will allow you to update your action to accept the actual object model instead of a string. 这将允许您更新操作以接受实际的对象模型而不是字符串。

public class UsersController : ApiController {

    //eg: POST api/users/post
    [HttpPost]
    public IHttpActionResult Post(PersonModel person) {
        if (person == null) return BadRequest();
        try
        {        
            SqlConnection connection = new SqlConnection("Data Source=198.71.226.6;Integrated Security=False;User ID=AtallahMaroniteDB;Password=a!m?P@$$123;Database=AtallahPlesk_;Connect Timeout=15;Encrypt=False;Packet Size=4096");

            String query = "INSERT INTO Members(LastName, FirstName, Gender, MobileNumber, EmailAddress, Job, Address) VALUES " +
            "(@LastName, @FirstName, @Gender, @MobileNumber, @EmailAddress, @Job, @Address)";
            SqlCommand command = new SqlCommand(query, connection);
            command.Parameters.Add("@LastName", person.LastName);
            command.Parameters.Add("@FirstName", person.FirstName);
            command.Parameters.Add("@Gender", person.Gender);
            command.Parameters.Add("@MobileNumber", person.MobileNumber);
            command.Parameters.Add("@EmailAddress", person.EmailAddress);
            command.Parameters.Add("@Job", person.Job);
            command.Parameters.Add("@Address", person.Address);

            connection.Open();
            command.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.InnerException.ToString());
            return InternalServerError();
        }
        return Ok();
    }
}

You also need to make sure the request sent is correct so you can get a match 您还需要确保发送的请求正确无误,这样您才能获得匹配

Here is a raw example request snippet 这是原始的示例请求片段

POST  /api/users/post HTTP/1.1
Host: http://www.mytestdomain.com
Content-Type: application/json
... 
Content-Length: 163

{"LastName":"Doe","FirstName":"Jane","Gender":"Female","MobileNumber":"+1234567890","EmailAddress":"jane.doe@example.com","Job":"Developer","Address":"My address"}

Try inspecting the requests sent from the mobile to make sure its being sent correctly. 尝试检查从移动设备发送的请求,以确保其发送正确。 Something like Fiddler. 像提琴手一样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未找到与请求URI&#39;Myurl&#39;匹配的HTTP资源在与请求相匹配的控制器&#39;controllername&#39;上未找到操作 - No HTTP resource was found that matches the request URI 'Myurl' No action was found on the controller 'controllername' that matches the request 未找到与Angular 2中的请求URI匹配的HTTP资源 - No HTTP resource was found that matches the request URI in Angular 2 获取没有与请求URI匹配的HTTP资源 - Getting No HTTP resource was found that matches the request URI 找不到与请求URI匹配的HTTP资源 - No HTTP resource was found that matches the request URI 找不到与webapi中的请求URI匹配的HTTP资源 - No HTTP resource was found that matches the request URI in webapi 未找到与请求URI匹配的HTTP资源,未找到与控制器匹配的类型 - No HTTP resource was found that matches the request URI, No type was found that matches the controller 没有找到与请求URI匹配的HTTP资源 - Web API + Angular - No HTTP resource was found that matches the request URI - Web API + Angular 自定义路由未找到与请求URI匹配的HTTP资源 - Custom Routes No HTTP resource was found that matches the request URI 在 WebAPI 和 AngularJS 中出现错误:“未找到与请求 URI 匹配的 HTTP 资源” - Getting the error: “No HTTP resource was found that matches the request URI” in WebAPI and AngularJS Web Api错误:“找不到与请求URI匹配的HTTP资源” - Web Api Error: “No HTTP resource was found that matches the request URI”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM