Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

c# - asp.net core 2.0 Unable to Post to database

I have a web application that is being developed on a windows env and runs on ubuntu 16.04.

I have no issues Posting info to my sqlite database file blog.db(located in the /. directory of the project ) in my windows environment, however when I try the same action on my ubuntu server, I get the following error:

Microsoft.AspNetCore.Server.Kestrel[17]
      Connection id "0HL8AR4JM7NOJ" bad request data: "Requests with 'Connection: Upgrade' cannot have content in the request body."
Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException: Requests with 'Connection: Upgrade' cannot have content in the request body.
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Frame.ThrowRequestRejected(RequestRejectionReason reason)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody.For(HttpVersion httpVersion, FrameRequestHeaders headers, Frame context)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Frame`1.<ProcessRequestsAsync>d__2.MoveNext()

The problem is, I'm not sure what is causing this error to occur. I don't think it is an issue with my code, but it is possible.

What do you guys think the problem is? Could this be caused by nginx? Or is this caused by asp.net?

Here is my Controller.cs

private ApplicationDbContext ctx = new ApplicationDbContext();

[HttpPost]
public IActionResult Sent(string name, string info, string email)
{
    var message = new ContactMessage
    {
        username = name,
        message = info,
        email = email,
        date = DateTime.Now
    };

    ctx.messages.Add(message);
    ctx.SaveChanges();
    return View();
}

ApplicationDb.cs

public class ApplicationDbContext : DbContext
{
    public DbSet<ContactMessage> messages { get; set; }
    public DbSet<Post> posts { get; set; }
    protected override void OnConfiguring(DbContextOptionsBuilder builder)
    {
        builder.UseSqlite("Filename=./blog.db");
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It was my nginx configuration.

within /./etc/nginx is a file called: nginx.conf

I had proxy_set_header Connection "upgrade";

when it should be proxy_set_header Connection $http_connection;

This fixed my problem and my database now works on the ubuntu side of things.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...