I'm playing around with Blazor in .net core 3 preview 5.
My solution is quite simple, with one project for the web api and a second for the blazor client app.
The api is currently serving on localhost:5000 and the blazor app on localhost:5001, and I'm using httpclient to make http calls to the rest endpoint.
One thing in struggling with is understanding the best way to avoid baking in the api server URLs into the blazor app.
Is there am established pattern which will enable be to specify the base url by config or run time?
Edit to add info from comment:
Ultimately, I'd like to run it in a container in our test k8s cluster, that means the target url for the api would change to (say) api.test.companydns.com. the blazor app will end up running inside a container, so ideally I could pass in the api url as a parameter. I wasnt sure of the best way to do that as the blazor code ends up being executed on the client side
Question:
Is there am established pattern which will enable be to specify the base url by config or run time?
Answer: Yes, the base Uri of your Blazor client side is determined by the <base> HTML element set in /wwwroot/index.html like so:
<base href="/" />
Hope this helps...
I've done a bit of a hack which works for me (posted more to show I'm putting effort into solving the problem for myself!!):
I've added a config file to wwwroot\config\config.json which contains:
{
"apiBaseUrl": "api.url.com"
}
Next, I've created an AppState.cs which used the HttpClient to download and store the base URL (Defaul DI instance of HttpClient has a BaseURL of the url serving the Blazor client app).
The AppState class is then made available using DI, so now all my Services can make calls to the new endpoint.
Lastly, I can use a build pipeline / CI / Docker volume / K8s config map to supply the relivent config.json depending on the hosting environment and without further changes to any Blazor app code.
Related
I am able to read URL in Configure method in startup class with help of middleware but I need to read URL in "ConfigureService" method which I am not getting any solution as of now. Is there any way to read application URL in "ConfigureService" method?
The URL that the web server listens on does not have to be the URL you want it to think it's on. It could be proxied.
You can't get the request pipeline in ConfigureServices, as the pipeline isn't even ready there.
The code at that point also doesn't have to be woken up from an external request, could be app pool initialization or a monitoring application.
Put the site URL in your appsettings, read it from there.
I have created a web application in .Net core(v5.0) and hosted it in Azure App Service. I have created a view and that allows me to add a new URL based on that create a new subdomain in the same service and publish code in that. This concept also uses in Jira software where our <projectname>.atlassian.com
Eg:
I have added dev in a text box then-new subdomain added like. dev.<myappservicename>.azurewebsites.net
In this case, all code copy and run this code properly.
Main Domain:
Base URL(Created URL): <myappservicename>.azurewebsites.net
Custom URL(Added from View): dev.<myappservicename>.azurewebsites.net
,
admin.<myappservicename>.azurewebsites.net
Technology Specification:
.Net Core(5.0)
C#
Azure App Service
If anyone has an idea then suggest thought.
It helps me a lot.
You can use restapi or .net sdk to create subdomain.
From your description, I see that there should be no need to redeploy your webapp, so if there is a business need, it is recommended to identify it through the program, what is the input url of the browser, to process your program.
Eg:
company1.<projec_tname>.azurewebsites.net
Get HttpRequest url to handle company1's bussiness.
company2.<projec_tname>.azurewebsites.net
Get HttpRequest url to handle company2's bussiness.
I can in ASP.NET and .NET Core create a selfhosted web service (WCF,REST, based on Kastrel or Katana and so on). But is there a way to create a full working web site, or SPA, may be based MVC ?
For example I can inside .NET Core Startup file add code:
app.Run(async (context) =>
{
await context.Response.WriteAsync($"Hello World! {
//insert your web site here :-)
}");
});
- but I should to create all html, js and so on code by myself. May be we have a better way ?
Of course I know that we have a big parts like ASP and so on - but we can't create selfhosted app, we need a web server - usually IIS !
I guess you mean that your self-hosted web server of visual studio is not accessible from the outside?
Assuming that your port forwarding and firewall are correctly configured. You need to change the bind address of your application:
new WebHostBuilder()
.UseKestrel()
...
.UseUrls("http://*:80")
...;
Note:
This should never be used without reverse proxy.
More info about why this should not be done without reverse proxy: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-2.0&tabs=aspnetcore2x
I have a single website that makes calls to multiple web-services, and the website is hosted in different environments, e.g:
https://production.domain.com
https://uat.domain.com
https://dev.domain.com
Each website calls out to webservices hosted in the same environment:
https://production.domain.com/rest/
https://uat.domain.com/rest/
https://dev.domain.com/rest/
How do I define my webservice URLs to be relative in the context of where the application is hosted? Do I require any IIS configuration to achieve this?
The result would be:
https://production.domain.com => https://production.domain.com/rest
Having multiple combinations of URLs in my web.config and IF statements in my code is last thing I want to consider doing.
If there's always a 1-to-1 relationship between the URL of the web service being called, and the URL of the request to the web application that's calling the web service then you should be able to compose the URL for the web service with relative ease. If you want a fully qualified URL, then you can start with the Request.Url for the incoming request to the website and for example:
var uriBuilder = new UriBuilder(Request.Url);
uriBuilder.Path = "/rest";
var uriForRestService = uriBuilder.ToString();
Using an instance of UriBuilder allows you to preserve the protocol, host and also any custom port (e.g. if you're working with Visual Studio/IIS Express and thus have a test.domain.local:4321 flavour of URL) although you could go down the route of just grabbing Request.Url.Authority and constructing the rest of the URL yourself if you have a need to.
I have a webforms application in a big solution folder with multiple projects. I wanted to consume a web api application which is also a part of the solution. So my client looks like as follows.
function GetText()
{
$.getJSON("api/SiteUsers",
function (data) {
$("#TestText").append(data);
});
}
and the controller is a simple string returning action.
[HttpGet]
public string TestText()
{
return "this is a text";
}
when I try to call the service I get a 404 error the following link could not be found
http://localhost:1234/MyAspxProj/MyFolder/MyPage/api/SiteUsers
I can understand it is probably because it is trying to find the resource from within the webforms application. How can I call the web api service? I am open to all suggestions and advice.
lets say your API is hosted on localhost:1111 and your webforms application is hosted on localhost:2222
first make sure you can get the results of the API you just created by going to:
localhost:1111/api/SiteUsers
once you are sure that the above URL is returning what you expect, you can be sure that your API is set-up correctly.
Now lets come to the next issue, accessing API from another application (i.e. not having the same Host as the API i.e. localhost2222)
To access APIs from an application that is on another domain, you need to enable CORS support on the WebAPI. There are manay resources on the internet that will explain you how you can achieve this: google for enabling cors in web api 2
Once you have set-up CORS on your web api project, you will be able to access your API from any application.
Remember: you only need to enable CORS if the client is on different domain AND the client is a web based client (which in your case it is i.e. web forms application)
I hope this will give you some direction.
Based on the info you've provided, it looks to me like you're not putting in the correct url. Assuming your [HttpGet] function is within a file at "api/SiteUsers", you would use a url like this: "api/SiteUsers/TestText"