how to post formdata or http.context.form data from angular 6 to asp.net mvc web api or api using Token based authentication with header

how to post formdata or http.context.form data from angular 6 to asp.net mvc web api or api using Token based authentication with header 


_____________________________________________________________________
Create form data :-

const formdata = new FormData();
    formdata.append('postJson', JSON.stringify(postJson));
_____________________________________________________________________
Attach Form data To Form-data object and call contract service to post data :-

this.PostService.Postdata(formdata).then((res: any) => {
      this.loggerService.info(res.status);
      this.responseStatus = res.status;
      this.responseDraftId = res.result.alfResponse.draftId;
      if (res.status === 200) {
        this.responseFilePath = res.result ? (res.result.FullFilePath ? res.result.FullFilePath : '') : '';
        this.showPopup('success', 'Success!', res.result.Reference + ' File has been created successfully.');
      } else {
        this.showPopup('error', 'Error!', 'Error while submit Contract!');
      }
    }).catch(error => {
      this.loggerService.error(error);
      this.showPopup('error', 'Error!', 'Error while submit Contract!');
    });
  }
______________________________________________________________________________
create Service "Post Service" with Promises :-

PostService(data: any): Promise<any> {
        return new Promise((resolve, reject) => {
            this.apiService.postWithformHeader('Contract/GenerateContract', data).subscribe((res: any) => {
                return resolve(res);
            }, (err) => {


                reject(err);
            });
        });
    }
//note apiservice is class name where this method is write
_________________________________________________________________________________

method for form data header Attach with Token :-

postWithformHeader(URL, data) {
    this.spinner.displaySpinner(true);
    const optionsData: any;
if(localstorage.getkey('mytoken'))
{
optionsData = {
      headers: this.createAuthorizationHeader(),
    };
}
else{
this.router.navigate(['login']);
}
    const response = this.http.post(this.appUrl + URL, data,optionsData).map((res => {
      this.spinner.displaySpinner(false);
      return res;
    })).catch((error: any) => {
      this.spinner.displaySpinner(false);
      return this.httpErrorHandler.handleError('api-service', error, URL);
    });

    return response;
  }
  _________________________________________________________________________________
 
  Method for create header :- 

   createAuthorizationHeader() {
    
    let headers = new HttpHeaders();
      headers = headers.append('authorization', 'Bearer ' + localstorage.getkey('mytoken'));
   
     return headers;
  }
  ________________________________________________________________________________

How to Add "App_Data" asp.net MVC folder (directory) in publish file

How to Add "App_Data"  asp.net MVC folder (directory) in publish file 




open

"Projectpath"\Properties\PublishProfiles

select File 

"MyHealthRecordApi.pubxml" file

it has following code 
_________________________________________________
<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your Web project. You can customize the behavior of this process
by editing this MSBuild file. In order to learn more about this please visit https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>True</ExcludeApp_Data>
    <publishUrl>D:\WebSite_Publish\MyHealthRecrodAPI_Publish</publishUrl>
    <DeleteExistingFiles>True</DeleteExistingFiles>
    <PrecompileBeforePublish>True</PrecompileBeforePublish>
    <EnableUpdateable>True</EnableUpdateable>
    <DebugSymbols>False</DebugSymbols>
    <WDPMergeOption>DonotMerge</WDPMergeOption>
  </PropertyGroup>
</Project>

update code 

<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your Web project. You can customize the behavior of this process
by editing this MSBuild file. In order to learn more about this please visit https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <publishUrl>D:\WebSite_Publish\MyHealthRecrodAPI_Publish</publishUrl>
    <DeleteExistingFiles>True</DeleteExistingFiles>
    <PrecompileBeforePublish>True</PrecompileBeforePublish>
    <EnableUpdateable>True</EnableUpdateable>
    <DebugSymbols>False</DebugSymbols>
    <WDPMergeOption>DonotMerge</WDPMergeOption>
  </PropertyGroup>
</Project>

Featured Post

how to post formdata or http.context.form data from angular 6 to asp.net mvc web api or api using Token based authentication with header

how to post formdata or http.context.form data from angular 6 to asp.net mvc web api or api using Token based authentication with header  ...

Popular Posts