Is there any way to get the WebContentPath
of a .Net Core API Project from another .Net Core Web Application project if both projects locate in the same solution?
I am uploading a profile photos to a folder say "Uploads" which is located in a .Net Core Web API project. I am calling the Web API controller method to save the profile photo from the .Net Core Web Application using Ajax call. The image is saved in my .Net Web API application successfully and its path is stored in SQL server database.
However, when I retrieve the image back for a specific user, I get the relative path to my .Net Core Web Application instead of path to my .Net Core Web API Project. Because, the actual images are stored on API Project rather than .Net Core Application.
Here is my code for retrieving the image:
`[Route("EditContact")]
[HttpPost]
public Contact EditContact(Contact contactObj)
{
Contact conObj = new Contact();
conObj.Id = contactObj.Id;
var contactDetails = _unitOfWork.Contact.GetContactById(conObj);
Contact returnObj = new Contact();
returnObj.FirstName = contactDetails.FirstName;
returnObj.LastName = contactDetails.LastName;
string imageUrl = contactDetails.ProfileImage;
returnObj.ProfileImage = "Uploads/" + imageUrl;
return returnObj;
}
The ProfileImage model field contains the exact path of the file which needs to be added to the Web API Project Path. However, on the front end, as the View is returned by .Net Core Web Application, so it the relative path to .Net Core Web Application is added to /Uploads/image.extention
which gives me error that the target file/folder is not available on API project.
How can I get the relative path of Web API Project in a .Net Core Web Application?
+1 for @Rena's answer. I was supposed to do something suggested in that answer. However, as per my application flow and because I was running everything on localhost, so I've used HelperMethod
where I statically saved the API URL and whenever I need a path to API, I used that path:
Because I am using Repository Pattern (The third project in my solution is repository), So I created a new folder in Repository Project and named it Helper. I added a class to the Helper folder called HelperPath.cs
and added the API URL there as follow:
public class HelperPath
{
//Assigned API URL to MainPath
public static string MainPath = @"https://localhost:****/";
//Host method I added later for adding the live API link when published from localhost to actual server
//public static string Host { get; set; }
}
Now, whenever I need to get something from API side, (In my case, the image path), I will do something like this:
[Route("EditContact")]
[HttpPost]
public Contact EditContact(Contact contactObj)
{
Contact conObj = new Contact();
conObj.Id = contactObj.Id;
var contactDetails = _unitOfWork.Contact.GetContactById(conObj);
Contact returnObj = new Contact();
returnObj.FirstName = contactDetails.FirstName;
returnObj.LastName = contactDetails.LastName;
string imageUrl = contactDetails.ProfileImage;
returnObj.ProfileImage = Repository.Helper.HelperPath.MainPath + "Uploads/" + imageUrl;
return returnObj;
}
However, one must configure the API Startup class such that the API project must use static files. In my case, I used the following code. Make sure to put it above app.UseHttpsRedirection();
in Configure Method
of startup.cs
class in API project.
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(env.ContentRootPath, "Uploads")),
RequestPath = "/Uploads"
});