首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将数据发布到控制器.net内核

将数据发布到控制器.net内核
EN

Stack Overflow用户
提问于 2019-07-18 20:51:56
回答 2查看 1.5K关注 0票数 3

我有以下控制器:

代码语言:javascript
复制
[Route("api/[controller]")]
[Authorize]
public class AuthenticationController : Controller
{
    private readonly IAuthenticationService _authenticationService;

    public AuthenticationController(IAuthenticationService authenticationService)
    {
        _authenticationService = authenticationService;
    }

    [AllowAnonymous]
    [HttpPost]
    public IActionResult Authenticate([FromBody] UserModel userParams)
    {
        var authenticate = _authenticationService.Authenticate(userParams.Username, userParams.Password);
        return Ok("");
    }

    [HttpGet]
    public IActionResult GetAll()
    {
        var users = "Greta";
        return null;
    }
}

我试着给邮递员发个帖子,但我得到的结果是:

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Error</title>
    </head>
    <body>
        <pre>Cannot POST /api/Authentication/Authenticate</pre>
    </body>
</html>

有人知道为什么我会得到这个错误吗?

我在Visual Studio中使用React-template。这会不会与此有关?

当我尝试访问GetAll端点时,我得到的结果是:

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <meta name="theme-color" content="#000000">
        <base href="/" />
        <!--
      manifest.json provides metadata used when your web app is added to the
      homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
    -->
        <link rel="manifest" href="/manifest.json">
        <link rel="shortcut icon" href="/favicon.ico">
        <!--
      Notice the use of  in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
        <title>ICOM.Cbs</title>
    </head>
    <body>
        <noscript>
      You need to enable JavaScript to run this app.
    </noscript>
        <div id="root"></div>
        <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
        <script type="text/javascript" src="/static/js/bundle.js"></script>
    </body>
</html>

编辑:它现在似乎可以工作了。我删除了FromBody,然后它就起作用了。我也尝试过使用FromForm,它很有效。我也不知道原因。

EN

回答 2

Stack Overflow用户

发布于 2019-07-18 21:00:57

当您仅在控制器中使用[FromBody],并且使用PostMan进行测试时。不使用form-data,而是使用raw并选择首选Content-Type: application/json

票数 1
EN

Stack Overflow用户

发布于 2019-07-18 21:12:21

我怀疑您只需要显式地指定操作的路由:

代码语言:javascript
复制
[AllowAnonymous]
[HttpPost(nameof(Authenticate))]
public IActionResult Authenticate([FromBody] UserModel userParams)
{
    var authenticate = _authenticationService.Authenticate(userParams.Username, userParams.Password);
    return Ok("");
}

请注意HttpPost属性中的路由,我刚刚将其设置为方法的名称。

如果只是在Startup.cs中使用.UseMvc(),则不会指定默认路由,因此需要为每个操作指定路由。如果您希望自动分配默认路由,则需要在Startup.cs中使用以下命令:

代码语言:javascript
复制
app.UseMvc(routes => {
   routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
});

但是,即使您这样做了,如果您在控制器级别指定了一个Route,默认路由也会从窗口中消失,因此您必须在该控制器中的每个操作上指定路由。

更多阅读:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57094906

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档