Search code examples
post.net-coregethttp-delete

getting error when call put, delete methods in dot net core


when call put, delete methods I get this error No 'Access-Control-Allow-Origin' but there is no error with get, post methods this occurs when publish on server but on my computer I don't have any errors this is code in startup file

 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            // 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.UseAuthentication();
        app.UseHttpsRedirection();
        app.UseCors("corsPolicy");

        app.UseStaticFiles();
        app.UseMvc();
    }

services.AddCors(options =>
        {
            options.AddPolicy("corsPolicy", builder =>
            {
                builder.AllowAnyMethod().AllowAnyOrigin().AllowAnyHeader().AllowCredentials();
            });
        });

controller code

namespace ContinuationProjLast.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [EnableCors("corsPolicy")]

public class marksController : ControllerBase
{
    private readonly ProductionRefContext _context;

    public marksController(ProductionRefContext context)
    {
        _context = context;
    }
[HttpPut("{id}")]

    public async Task<IActionResult> Putmark(int id, mark mark)
    {
        if (id != mark.ID)
        {
            return BadRequest();
        }

        var mm = _context.mark.Where(x => x.devicemark == mark.devicemark && x.devicecategory == mark.devicecategory).FirstOrDefault();
        if (mm == null)
        {

            _context.Entry(mark).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!markExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }
            return NoContent();
        }
        else
        {
            return BadRequest();

        }

    }

the same thing happens with delete method.

Can I get any help?


Solution

  • I removed WebDAV publishing from iis , Go to control panel > Programs and Features > turn windows features on or off >IIS > World wide web services > Common HTTP Features > WebDAV publishing
    the problem has been solved