Wasif Zaman
Wasif Zaman

Reputation: 33

getting status: 404, statusText: "Not Found", url ... in asp.net using core webApi?

Getting 404 error in ASP.NET Core Web API. With ASP.NET I am using Angular 7 for front end. When I click on save button of a form, the form data reaches to post method in angular but after click on save it shows 404 error. For this form I created first model where I define its property for database then I created controller for that model and use that property that I declared in model.

Angular service ts file:

export class MurderService {
   BaseUrl :string ='';
  constructor( private http:HttpClient, private config:ConfigService) {
    this.BaseUrl=config.getApiURI();
   }

  murderQuestionnaire(data: any){
     var murderBody={
       Dead: data.Dead,
       Wounded: data.Wounded,
       CriminalsInvolved: data.CriminalsInvolved,
       CriminalAppearance: data.CriminalAppearance,
       VehiclesUsed: data.VehiclesUsed,
       WeaponsDescription :data.WeaponsDescription
     };
     return this.http.post(this.BaseUrl +'/Complians' , murderBody);//upto here data reaches 
      successfully
  }
}

API controller for this service:

    [Route("api/[controller]")]
    [ApiController]
    public class ComplainMurderController : ControllerBase
    {
        [HttpPost]
        [Route("Complians")]
        //api:/Complians
        public void PostComplainMurder(Complians complian)
        {
            var complianMurder = new Complians()
            {
                Dead = complian.Dead,
                Wounded = complian.Wounded,
                CriminalsInvolved = complian.CriminalsInvolved,
                CriminalAppearence = complian.CriminalAppearence,
                VehiclesUsed = complian.VehiclesUsed,
                WeaponsDescription = complian.WeaponsDescription
            };
            try
            {
               // var result = await complian.Add(complianMurder);
                AuthenticationContext authenticationContext = new AuthenticationContext();
                authenticationContext.Add(complianMurder);
                authenticationContext.SaveChanges();
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
    }
}

Error

POST http://localhost:49601/api/Complians 404 (Not Found)

Upvotes: 0

Views: 651

Answers (1)

Timmeh
Timmeh

Reputation: 408

Your controller route is defined as api/[controller] (in your case this leads to api/ComplainMurder). Your action is defined as Complians. These combined is your actual route: api/ComplainMurder/Complians, which does not match api/Complians.

Changing your Angular 7 side with this will fix it:

return this.http.post(this.BaseUrl +'/ComplainMurder/Complians' , murderBody);

Side note: be careful with incorrect/inconsistent spelling (complian instead of complain) as it's bound to lead to confusion and/or mistakes in the future.

Upvotes: 1

Related Questions