How can i get base url of a c# minimal api application?

huangapple go评论60阅读模式
英文:

How can i get base url of a c# minimal api application?

问题

我有一个最小化的 API 应用程序,我想返回一个文件路径,我找到的大部分答案都是针对基于控制器的 API,它们使用 Request 来获取 URL。

英文:

i have a minimal api application and i would like to return a path to file, most of the answers i found are for controller based APIs that use the Request to get the url from.

答案1

得分: 1

如果您正在使用最小的API应用程序,并希望返回文件的路径,您可以使用ASP.NET Core中可用的HttpContext类。

以下是一个示例:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/file", async context =>
{
    // 指定文件的路径
    string filePath = "/path/to/file.txt";

    // 根据文件扩展名设置内容类型
    context.Response.ContentType = "text/plain";

    // 将文件内容写入响应
    await context.Response.SendFileAsync(filePath);
});

app.Run();
英文:

If you're working with a minimal API application and you want to return a path to a file, you can use the HttpContext class available in ASP.NET Core.

Here's an example:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/file", async context =>
{
    // Specify the path to the file
    string filePath = "/path/to/file.txt";

    // Set the content type based on the file extension
    context.Response.ContentType = "text/plain";

    // Write the file content to the response
    await context.Response.SendFileAsync(filePath);
});

app.Run();

答案2

得分: 1

要获取C# Minimal API应用程序的基本URL,您应该在API中提取httpContext,它具有名为Request.HostRequest.Path的属性,这将返回应用程序的基本URL。

在Minimal API中获取基本URL:

让我们看看如何在实践中提取它:

app.MapGet("/baseurl", (HttpContext context) =>
{
    var baseURL = context.Request.Host;
    var basepath = context.Request.Path;
    return Results.Ok($"Base URL: {baseURL} and base path: {basepath} thus, full path: {baseURL + basepath}");
});

输出:

How can i get base url of a c# minimal api application?

How can i get base url of a c# minimal api application?

注意: 有关更多详细信息,请参阅官方文档。此外,可以在此处找到示例

英文:

> How can i get base url of a c# minimal api application?

In order to get, base url within minimal api you should extract the httpContext within your API and it has the property called Request.Host and Request.Path which will return you application base URL.

Base URL In Minimal API:

Let's have a look how we can extract that in practice:

  app.MapGet("/baseurl", (HttpContext context) =>
    {
        var baseURL = context.Request.Host;
        var basepath = context.Request.Path;
        return Results.Ok($"Base URL: {baseURL} and base path: {basepath} thus, full path: {baseURL + basepath}"); 
    });

Output:

How can i get base url of a c# minimal api application?

How can i get base url of a c# minimal api application?

Note: Please refer to the official document for more details here. In addition, sample can be found here.

huangapple
  • 本文由 发表于 2023年7月3日 08:02:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601210.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定