0

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)

3
  • Probably because your route is defined as Complians but you are calling api/Complians. Commented Nov 5, 2019 at 12:05
  • @lan Kemp upto api its the asp.net url and /complians its table in database. Commented Nov 5, 2019 at 12:22
  • Mixing "Complian" and "Complain" everywhere is bound to lead to mistakes. Commented Nov 5, 2019 at 12:33

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

you means that i need to change api/[controller] to api/[ComplianMurder].?
Your action route is different from your Angular post URL. You have to change either side so they match. return this.http.post(this.BaseUrl +'/ComplainMurder/Complians' , murderBody);
@WasifZaman There is a button to press to accept an answer. It should be to the left of my answer and look like a check mark (✓)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.