简体   繁体   English

ASP.NET MVC API发布数据

[英]ASP.NET MVC API Post Data

I have an API in MVC. 我在MVC中有一个API。 It is configured as follows: 它的配置如下:

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

I am using Custom routing for my API to make restful services an Example is the following Controller 我正在使用API​​的自定义路由来提供静态服务,例如以下控制器

[RoutePrefix("datapi/user")]
public class UserController : ApiController
{
    [EnableCors("*", "*", "*")]
    [Route("getuser/{userid}/")]
    [System.Web.Http.HttpGet]
    public ObjUser GetUserObject(string userid)
    {
        ObjUser user = this.userdata.GetUser(userid);
        return user;
    }

    [EnableCors("*", "*", "*")]
    [Route("updatepassword")]
    [System.Web.Http.HttpPost]
    public ObjUser UpdateUserPassword(string userid, string password)
    {
        ObjUser user = this.userdata.GetUser(userid);
        user.Password = password;
        this.userdata.SaveUser(user);
        return user;
    }
}

The 'getuser' method works fine. 'getuser'方法工作正常。 I get the Object in Json Format in my applicaiton 我在应用程序中获得了Json格式的对象

The trouble arises when I use HttpPost service (updatepassword). 当我使用HttpPost服务(updatepassword)时会出现问题。 I trying to send data via HttpPost and it gives me a CORS error which says 我试图通过HttpPost发送数据,这给了我一个CORS错误,提示说

"Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource" “跨域请求被阻止:同源策略禁止读取远程资源”

Is there something wrong with my config? 我的配置有问题吗?

The following is the test I have done for both 以下是我对两者所做的测试

$.ajax({
  url: 'http://localhost:3351/datapi/user/updatepassword'
  type: 'POST',
  data: {
    userid: 'joe.brandt@email.com',
    password: 'newpassword'
  },
  contentType: 'application/json',
  success: function(data) {
    console.log(data);
  }
});

$.ajax({
  url: 'http://localhost:3351/datapi/user/getuser/joe.brandt@email.com'
  type: 'GET',
  contentType: 'application/json',
  success: function(data) {
    console.log(data);
  }
});

EDIT: MORE TEST RESULTS 编辑:更多测试结果

So I had a hunch and I tested the service as follows: 因此,我有一种预感,我对服务进行了如下测试:

$.ajax({
  url: 'http://localhost:3351/datapi/user/updatepassword/joe.brandt@email.com/somepassword'
  type: 'POST',
  contentType: 'application/json',
  success: function(data) {
    console.log(data);
  }
});

This worked. 这工作了。 But, if anyone of the inputs contains a colon ":" it gives a CORS error! 但是,如果任何输入中包含冒号“:”,则会出现CORS错误! The way forward is two paths, one restrict input of ":" and the second one is convert it into unicode. 前进的途径有两条,一条限制输入“:”,第二条将其转换为unicode。 The API is restful but some other guy suggessted to give data in Ajax data as payload, will that work with restful api? 该API是宁静的,但是有人建议将Ajax数据中的数据作为有效负载提供,这是否适用于宁静的api?

尝试将以下代码行添加到WebApiConfig.cs的Register方法中

config.EnableCors(new EnableCorsAttribute("*", "*", "POST"));

So the solution that we have reached to is to make a complex structure with userid and password as it's properties and we have changed the method like this 因此,我们要解决的问题是使用用户名和密码作为属性来构建一个复杂的结构,并且我们更改了这种方法

public ObjUser UpdateUserPassword(ApiUserParam userParam)
{
    ObjUser user = this.userdata.GetUser(userParam.userid);
    user.Password = userParam.password;
    this.userdata.SaveUser(user);
    return user;
}

And now I have a working web service with Post and completely restful too. 现在,我可以在Post上使用可正常使用的Web服务,并且也完全可以使用。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM