简体   繁体   中英

Why the postman is returning a “Non-static method requires a target.”

I have a Save method in my SavesController (Web Api controller). When I tested it with postman it returned this.

    {
"message": "An error has occurred.",
"exceptionMessage": "Non-static method requires a target.",
"exceptionType": "System.Reflection.TargetException",
"stackTrace": "   at System.Web.Http.ApiController.    <InvokeActionWithExceptionFilters>d__1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext()"
  }

My save method is this. Exception was thrown at if statement

[HttpPost]
public IHttpActionResult Save(SavesDto dto)
{
    var userId = User.Identity.GetUserId();

    if (_context.SavedPosts.Any(a => a.AttendeeId == userId && a.PostId == dto.PostId))
    return BadRequest("The save already exists.");

    var savedPost = new Saves
    {
        PostId = dto.PostId,
        AttendeeId = userId
    };
    _context.SavedPosts.Add(savedPost);
    _context.SaveChanges();

    return Ok();
}

I have searched through the internet.I found out that problem comes from using a null value within the LINQ. But Any method in Linq statement just returns true/false, why is it throwing this exception?

    public class SavesDto
{
    public int PostId { get; set; }
}

I called the Api here

@foreach(var post in Model)
{
<button
 data-post-id="@post.Id" 
class="btn btn-default btn-sm pull-right .js-toggle-save">Save ?</button
}

@section scripts
{
  <script>
$(document).ready(function()
{
    $(".js-toggle-save").click(function(e)
    {
        var button = $(e.target);
        $.post("/api/saves", { postId: button.attr("data-post-id") } )
        .done(function()
        {
            button.removeClass("btn-default")
            .addClass("btn-info").text("Saved");
        })
        .fail(function ()
        {
            alert("something failed");
        })
    });
}
);
</script>
}

PostId and AttendeeId are not nullable

public class Saves
{
    public ApplicationUser User { get; set; }
    public Post Post { get; set; }

    [Key]
    [Column(Order = 1)]
    public int PostId { get; set; }

    [Key]
    [Column(Order = 2)]
    public string AttendeeId { get; set; }

}

This exception occurs when you use a variable in a lambda expression which is null. It appears that the dto object is null. So try doing this:

if (dto != null && _context.SavedPosts.Any(a => a.AttendeeId == userId
  && a.PostId == dto.PostId))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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