ASP .Net Core 2.2 publish to Azure - asp.net

I am new to working with Azure, but recently I have published my ASP.Net core 2 app to App Service and cannot access Email templates (.html) from within the API.
API works fine, it connects to the client side and to the database, but when I try and send out an e-mail - I get exception:
Could not find a part of the path 'D:\home\site\{ProjectName}\EmailTemplate\template.html'.
From within the code I call template using the following path format:
../{ProjectName}/EmailTemplate/template.html
How do I access these templates on a hosted environment?
Many thanks,
Alex

As per comments.
Right-click on the html template in vs and click Properties, then change Copy to Output Directory > Copy Always.
Also, as #jpgrassi noted, you can also add patterns in your csproj file to include a whole directory or even all files that end with .html. Just right-click on your project and select Edit <Project Name>. Then just add this:
<ItemGroup>
<Content Include="EmailTemplate\*.html" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

Related

ASP.NET 4.7 - Subfolder inside Content not deployed to cloud

I have just deployed my first ASP.NET 4.7 Application to Azure but on the deployed website the background image is missing. The background image loads without problem on local server and it is located in /MyApplication/Content/Images/bg.jpg.
I tried to log on to Azure console and found that in fact the entire Images folder is not uploaded (but the other files in the Content folder are there).
How do I include the Images folder when deploying to Azure? Is there some settings that I need to adjust during deployment or codes that I need to change in my Application?
Thank you.
After reproducing from our end we have observed that in order to make the folder content visible it needs to hold some files. We have manually added files by navigating through folder explorer but couldn't able to see any file being added in VS. So after adding the below lines to .csproj we could able to see the folder and its contents being added after deployment.
<ItemGroup>
<Content Include="Content\Images\**\*" />
</ItemGroup>
RESULT:
REFERENCES: How to include custom folders when publishing a MVC application?

How to handle path on azure for XmlDocumentationProvider

I am using azure for deployment of my new Web API's, I am new with deployment on IIS and azure.
I have added my Web API's on azure as web application, and it's working fine, till I added documentation for each API's functions. After adding description I uncomment line below from HelpPageConfig.cs.
config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));
It's working finr locally, and I am able to see all descriptions, but when I published it on azure, I am getting error saying,
Could not find a part of the path 'D:\home\site\wwwroot\App_Data\XmlDocument.xml'.
site url : http://mejodo.azurewebsites.net/
Do I need to change path ?
File is already created in my system on D:\home\site\wwwroot\App_Data directory.
What changes I need to do to make it work ?
I had the same issue. For me the file was generated. Please follow the below steps.
Click on show all files in solution explorer.
Check whether you have a file in App_Data folder
If you have the file, right click and include the same in your project.
Now build and publish to Azure.
I hope this will work. Thanks
In my case XmlDocument.xml had to be added in Visual Studio to the App_Data folder within the solution using 'add existing item'.
Just add to your project new folder "App_Data" with XmlDocument.xml and publish
When you say that you have Web API's on azure as web application, do you mean that they are applications under the site ? If yes, then I think you are missing the application name in the path to the XML file.
Try going to mejodo.scm.azurewebsites.net > debug console > powershell to see the exact folder structure....
You must verify that the line in the HelpPageConfig.cs file in the Area>HelpPage>App_Start folder.
that is in the register method, this line is
config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));
this uncommented
then you must verify that in the project configuration, in the build XML documentation file, this is the same path as the above mentioned line.
When you publish the xml file goes in the bin folder, so change 'D:\home\site\wwwroot\App_Data\XmlDocument.xml'.
to
'D:\home\site\wwwroot\bin\XmlDocument.xml'.
and it should work.

How to deploy XML file with my web application?

I've written an ASP.net web application. In the interest of following the advice in "The Pragmatic Programmer" to put application logic in configuration, I wrote a large XML file that describes various business rules.
When I test the application on my local development workstation, I copy the file to c:\xxxxx\myfile.xml and then write the code to read the file from this location.
What is the correct way to deploy this xml file as part of my web application so that it gets read from the directory in which the web application is deployed?
Update: The XML file in question is for server-side configuration and should never be available for download to the end-user.
If you're not embedding this as a resource, you should:
Create a directory under which this file will reside.
Create the file and set its Build Action to Content.
Create a web.config file in that directory which forbids access to the directory so you don't expose your business rules to the Internet.
Add a setting in your application's main web.config that gives the path to this file relative to the root of the application, i.e. "~/MySecureFolder/MyBusinessRules.Xml".
Have some code that calls HttpServerUtility.MapPath Method to convert the value from the setting in web.config to a virtual path.
I don't know if this is what you want - Click on the XML file, then open the Property Window and find the "Build Action" property. Set the value to "Embedded Resources"
I think what you need to do is:
Add the XML file to your web application project in Visual Studio
Right click on the file and select 'Properties...'
Set the Build Action to 'Content' and Copy to Output Directory to 'Do not copy'
this will ensure that your XML file is deployed along with the rest of your web app.
If you want to make available your XML file from http requests to your server, you should
place it in your web publication folder.
This ASP instruction should help you to find your publication path:
Request.ServerVariables("APPL_PHYSICAL_PATH")

Is it possible to modify web.config of existing site using MSDeploy?

Is it possible to modify (or just replace) web.config of existing site using MSDeploy?
It's possible to replace certain sections (specified with an xPath query or regular expression) of the web config file. Use the -declareParam en -setParam commandline switches for that.
Like so
msdeploy -verb:sync -source:apphostconfig="Default Web Site" -dest:package=ParameterPackage.zip -declareParam:name=param,kind=XmlFile,scope=web.config,match=//add/#value
or so:
msdeploy -verb:sync -source:package=ParameterPackage.zip -dest:auto -setParam:name=param,value=MyDefaultWebPage.htm
You can find more info here if you're using the command line.
If your working with importing and exporting packages in and from IIS you can create a parameters.xml file. Vishal Joshi has lots of good posts about how to use msdeploy (for example this)
Yes you can do this. I just posted a blog on this at http://sedodream.com/2012/02/14/HowToUpdateASingleFileUsingWebDeployMSDeploy.aspx but I'm also copying the content below for you.
The other day I saw a question posted on StackOverflow asking if it was possible to update web.config using MSDeploy. I actually used a technique where I updated a single file in one of my previous posts at How to take your web app offline during publishing but it wasn’t called out too much. In any case I’ll show you how you can update a single file (in this case web.config) using MSDeploy.
You can use the contentPath provider to facilitate updating a single file. Using contentPath you can sync either a single file or an entire folder. You can also use IIS app paths to resolve where the file/folder resides. For example if I have a web.config file in a local folder named “C:\Data\Personal\My Repo\sayed-samples\UpdateWebConfig” and I want to update my IIS site UpdateWebCfg running in the Default Web Site on my folder I would use the command shown below.
%msdeploy% -verb:sync -source:contentPath="C:\Data\Personal\My Repo\sayed-samples\UpdateWebConfig\web.config" -dest:contentPath="Default Web Site/UpdateWebCfg/web.config"
From the command above you can see that I set the source content path to the local file and the dest content path using the IIS path {SiteName}/{AppName}/{file-path}. In this case I am updating a site running in IIS on my local machine. In order to update one that is running on a remote machine you will have to add ComputerName and possibly some other values to the –dest argument.
You can view the latest sources for this sample at my github repo.

ASP.NET Setup Project - how to include static files not in VS?

I am working on an ASP.NET MVC web application, and am working on the Web Setup portion. We are using SVN for version control. One of the issues is that the we are currently having is that the web designers modify and add a lot of html, css and js files that end up in the Content folders, but they don't add them to the VS project, so the new files don't get included in the installer.
One option is that we have to try to teach them to always go into VS and manually add the files, but we're on a large project and it's getting down to crunch time, so trying to get people to learn something new in a technology that they don't know will result in too many mistakes, so I'd like to work around that if possible.
Is there some way to get the setup project just to include files from a folder on the file system, instead of a project's designated content files?
Thanks!
I think I've figured it out:
Make sure that the installer project includes the Content Files for the web app. Then go into the web app's csproj file with a text editor and find the ItemGroup where the static Content files are listed. Then add an entry with a wildcard in it and the installer will package them in.
<ItemGroup>
<Content Include="Content\test.html" />
<Content Include="Content\*.html" />
<Content Include="Content\*.gif" />
<Content Include="Content\*.css" />
<Content Include="Content\*.jpg" />
<Content Include="Content\*\*.js" />
Right click on the folder in the File System view of the Web Setup project, and select Add, File...
It doesn't look like you can add an entire folder this way though, so you would have to add every file individually to the Setup program.
EDIT:
Aren't all files from your project folder included in a WebSite project in Visual Studio? Then it's just a case of having your WebSite project and Web Setup project in the same solution, then right-click on the Web Application Folder in the Web Setup project, select Add, Project Output... and then select the WebSite Project in the drop down and Content Files from the list box and click OK.
Unfortunately I don't think there is an out of the box way to do this, as projects maintains references to the files individually.
You could achieve what you need via a custom macro, or asking your other team to edit the .csproj XML file (perhaps simpler or riskier, depending on their background).

Resources