C# I';当我试图上传一篇关于我的项目的帖子时,我遇到了一个405错误

C# I';当我试图上传一篇关于我的项目的帖子时,我遇到了一个405错误,c#,asp.net-mvc,model-view-controller,asp.net-mvc-5,asp.net-core-mvc,C#,Asp.net Mvc,Model View Controller,Asp.net Mvc 5,Asp.net Core Mvc,在我的开发博客上上传帖子时遇到问题。我正在尝试使用MVC框架上传帖子。关于如何建立一个博客,我试图遵循大量的指南 这是邮政课 using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Threading.

在我的开发博客上上传帖子时遇到问题。我正在尝试使用MVC框架上传帖子。关于如何建立一个博客,我试图遵循大量的指南

这是邮政课

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;

namespace ProjectWebApp.Models
{
    public class Post
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int PostID { get; set; }
        [Required]
        public string Title { get; set; }
        [Required]
        public string Content { get; set; }
        public string Author { get; set; }
        public int Likes { get; set; }
        [Required]
        public DateTime DateCreated { get; set; }
        public DateTime? DateUpdated { get; set; }
        public ICollection<PostTag> PostTags { get; set; }
    }
}

使用系统;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.ComponentModel.DataAnnotations.Schema;
使用System.Linq;
使用System.Threading.Tasks;
命名空间ProjectWebApp.Models
{
公营职位
{
[关键]
[数据库生成(DatabaseGeneratedOption.Identity)]
公共int PostID{get;set;}
[必需]
公共字符串标题{get;set;}
[必需]
公共字符串内容{get;set;}
公共字符串作者{get;set;}
公共int类{get;set;}
[必需]
public DateTime DateCreated{get;set;}
公共日期时间?日期更新{get;set;}
公共ICollection后标记{get;set;}
}
}
以下是BlogDBContext:


using Project.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ProjectBlogWebApp.Data
{
    public class BlogDbContext : DbContext
    {
        public BlogDbContext(DbContextOptions<BlogDbContext> options) : base(options)
        {
            Database.EnsureDeleted();
            if (Database.EnsureCreated() == true)
            {
                Database.EnsureCreated();
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<PostTag>().HasKey(p => new {p.PostID, p.TagID});
            modelBuilder.Entity<PostTag>().HasOne(pt => pt.Post).WithMany(p => p.PostTags)
                .HasForeignKey(pt => pt.PostID);
            modelBuilder.Entity<PostTag>().HasOne(pt => pt.Tag).WithMany(t => t.PostTags)
                .HasForeignKey(pt => pt.TagID);
        }



        public DbSet<Post> Posts { get; set; }

        public DbSet<Tag> Tags { get; set; }

        public DbSet<PostTag> PostTags { get; set; }
    }
}


使用项目模型;
使用Microsoft.EntityFrameworkCore;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
命名空间ProjectBlogWebApp.Data
{
公共类BlogDbContext:DbContext
{
公共BlogDbContext(DbContextOptions选项):基本(选项)
{
数据库。确保已删除();
if(Database.recreated()==true)
{
数据库。请重新创建();
}
}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
modelBuilder.Entity().HasKey(p=>new{p.PostID,p.TagID});
modelBuilder.Entity().HasOne(pt=>pt.Post).WithMany(p=>p.PostTags)
.HasForeignKey(pt=>pt.posted);
modelBuilder.Entity().HasOne(pt=>pt.Tag).WithMany(t=>t.PostTags)
.HasForeignKey(pt=>pt.TagID);
}
公共DbSet Posts{get;set;}
公共DbSet标记{get;set;}
公共DbSet PostTags{get;set;}
}
}
以下是PostController类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ProjectWebApp.Data;
using ProjectWebApp.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.EntityFrameworkCore;

namespace ProjectWebApp.Controllers
{
    public class PostController : Controller
    {
        private BlogDbContext _dbBlogContext;

        public PostController(BlogDbContext dbContext)
        {
            _dbBlogContext = dbContext;
        }

        public IActionResult Index()
        {
            var postList = _dbBlogContext.Posts.ToList();

            return View(postList);
        }
        [HttpGet, Route("Create")]
        public IActionResult Create()
        {
            return View(new Post());
        }

        [HttpGet, Route("Edit")]
        public IActionResult Edit()
        {
            
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> CreatePostAsync([Bind("Title", "Content")] Post post)
        {
            try 
            {
                post.Likes = 0;
                post.DateCreated = DateTime.Now;
                post.Author = "Leonard Morrison";
                _dbBlogContext.Add(post);
                await _dbBlogContext.SaveChangesAsync();

                
            }

            catch (DbUpdateException)
            {
                ModelState.TryAddModelError( "Error: Post was not added properly!", "Sorry, the Post was not added properly. Please let me know if this problem persists");
            }

            return View(post);
        }

        [HttpGet]
        public IActionResult Show(int ID)
        {
            var post = getPost(ID);
            return View(post);
        }

        [HttpGet]
        public IActionResult Edit(int ID)
        {
            var post = getPost(ID);
            return View(post);
        }
        [HttpPatch]
        public IActionResult Update(int id)
        {
            var post = _dbBlogContext.Posts.Find(id);
            _dbBlogContext.Posts.Update(post);
            return RedirectToAction("Index");
            
        }

        [HttpDelete]
        public IActionResult RemovePost(int id)
        {
            Post deletedPost = getPost(id);

            _dbBlogContext.Posts.Remove(deletedPost);

            _dbBlogContext.SaveChanges();

            return RedirectToAction("Index");
        }


        public Post getPost(int ID)
        {
            var post = _dbBlogContext.Posts.First(p => p.PostID == ID);

            return post;
        }

    }
}


使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用ProjectWebApp.Data;
使用ProjectWebApp.Models;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.AspNetCore.Mvc.ModelBinding;
使用Microsoft.EntityFrameworkCore;
命名空间ProjectWebApp.Controllers
{
公共类后控制器:控制器
{
私有blogdbContextu dbBlogContext;
公共PostController(BlogDbContext)
{
_dbBlogContext=dbContext;
}
公共IActionResult索引()
{
var postList=_dbBlogContext.Posts.ToList();
返回视图(postList);
}
[HttpGet,路由(“创建”)]
public IActionResult Create()
{
返回视图(newpost());
}
[HttpGet,路由(“编辑”)]
公共IActionResult编辑()
{
返回视图();
}
[HttpPost]
公共异步任务CreatePostAsync([Bind(“Title”、“Content”)]Post-Post)
{
尝试
{
post.Likes=0;
post.DateCreated=DateTime.Now;
post.Author=“伦纳德·莫里森”;
_dbBlogContext.Add(post);
wait_dbBlogContext.SaveChangesAsync();
}
捕获(DbUpdateException)
{
ModelState.tryaddmodeleror(“错误:未正确添加帖子!”,“抱歉,帖子未正确添加。如果此问题仍然存在,请通知我”);
}
返回视图(post);
}
[HttpGet]
公共IActionResult Show(内部ID)
{
var post=getPost(ID);
返回视图(post);
}
[HttpGet]
公共IActionResult编辑(内部ID)
{
var post=getPost(ID);
返回视图(post);
}
[HttpPatch]
公共IActionResult更新(内部id)
{
var post=_dbBlogContext.Posts.Find(id);
_dbBlogContext.Posts.Update(post);
返回操作(“索引”);
}
[HttpDelete]
公共IActionResult RemovePost(内部id)
{
Post deletedPost=getPost(id);
_dbBlogContext.Posts.Remove(deletedPost);
_dbBlogContext.SaveChanges();
返回操作(“索引”);
}
公共Post getPost(内部ID)
{
var post=_dbBlogContext.Posts.First(p=>p.PostID==ID);
回程站;
}
}
}
最后,这里是启动源代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ProjectWebApp.Data;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Http;


namespace ProjectBlogWebApp
{
    public class Startup
    {



        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();


            services.AddDbContext<BlogDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddScoped<BlogDbContext, BlogDbContext>();

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            //The Main EndPoint Routes
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id}");
            });
            
            //The Post Endpoints Routes
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(name: "post",
                    pattern: "{controller=Post}/{action=Index}/{title?}");

            });
        }
    }
}


使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用ProjectWebApp.Data;
使用Microsoft.AspNetCore.Builder;
使用Microsoft.AspNetCore.Hosting;
使用Microsoft.AspNetCore.HttpsPolicy;
使用Microsoft.Extensions.Configuration;
使用Microsoft.Extensions.DependencyInjection;
使用Microsoft.Extensions.Hosting;
使用Microsoft.EntityFrameworkCore;
使用Microsoft.AspNetCore.Http;
命名空间ProjectBlogWebApp
{
公营创业
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
services.AddControllersWithViews();
services.AddDbContext(options=>options.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”));
services.addScope();
}
//此方法由运行时调用。请使用此方法配置
[HttpPost]
[Route("Create")]
public async Task<IActionResult> CreatePostAsync([Bind("Title", "Content")] Post post)