I have created API in .Net Framework4.5 and It's working fine in Postman but When i implenting with Angular7 then we didn't get Requested Parameters like(Username and Pass) in API.
I have already tried these steps:
I have already istalled this packege Microsoft.AspNet.WebApi.Cors
DemoController.cs
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Threading.Tasks;
- using System.Web;
- using System.Web.Http;
- using System.Web.Http.Cors;
- using System.Web.Http.Description;
- using TestData.Models;
- namespace TestData.Controllers
- {
- public class DemoController : ApiController
- {
- [HttpPost]
- [Route("api/Demo/Login")]
- public IHttpActionResult Login(HttpRequestMessage request)
- {
- string username = HttpContext.Current.Request.Form["Username"]; // getting Null
- string pass = HttpContext.Current.Request.Form["Pass"]; // getting Null
- return Ok('Username: ' +username + 'Password :' +pass);
- }
- }
- }
WebApiConfig.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web.Http;
- using System.Web.Http.Cors;
- namespace TestData
- {
- public static class WebApiConfig
- {
- public static void Register(HttpConfiguration config)
- {
- // Web API configuration and services
- // Web API routes
- config.MapHttpAttributeRoutes();
- config.Routes.MapHttpRoute(
- name: "DefaultApi",
- routeTemplate: "api/{controller}/{id}",
- defaults: new { id = RouteParameter.Optional }
- );
- EnableCorsAttribute cors = new EnableCorsAttribute("*", "*", "*");
- config.EnableCors(cors);
- }
- }
- }
I am using some code of Angular
auth.service.ts
- import { Injectable } from '@angular/core';
- import { HttpClient, HttpHeaders } from '@angular/common/http';
- @Injectable({
- providedIn: 'root'
- })
- export class AuthService {
- apiUrl : any = 'http://mydomain/api';
- constructor(private http : HttpClient) { }
- GetHttpHeaders() : HttpHeaders{
- const headers = new HttpHeaders().set('Content-Type', 'application/json');
- return headers;
- }
- loginUser(){
- var data = JSON.stringify({
- "Username" : '3333',
- "Pass" : '123456'
- })
- return this.http.post(this.apiUrl+'/Demo/Login', data, { headers : this.GetHttpHeaders() }).subscribe((results) => {
- console.log(results);
- });
- }
- }
Rajeesh MenothPosted Aug 19, 2019, 11:52 PM
Posted Aug 19, 2019, 11:14 PM
Bidyasagar MishraPosted Aug 16, 2019, 8:52 PM
Veerendra AnnigerePosted May 31, 2019, 2:14 AM
Include a file named .htaccess (notice the leading dot) anywhere in the path of the requested file, that file should contain this line:
Header set Access-Control-Allow-Origin "*".
Madan ShekarPosted May 31, 2019, 2:06 AM
My question is in case of API's are deployed at Godaddy or some other servers , in such case also we will get same configuratoion section.
Veerendra AnnigerePosted May 31, 2019, 2:01 AM
Madan ShekarPosted May 29, 2019, 12:10 AM
If it helps pls mark as answer.
Gajendra JangidPosted May 29, 2019, 12:05 AM