.net 6 minimal api self-host/access via ip

GrimOfDoom 101 Reputation points
2021-12-12T09:16:23.11+00:00

I am new to asp.net with the new minimal api (coming from node.js), and I want to package up my web app to be accessible by lan ipv4, but so far I am stuck with only the computer running the program to access it by localhost:xxxx (not even 192.168.1.2:xxxx on the computer running it will work. No errors popping up on console). My program is designed to be local use only, with up to several other browsers accessing it at once.

I did find this article once mentioning iisintegration & kestrel, but never once again does it mention them.

Developer technologies ASP.NET ASP.NET Core
Developer technologies C#
{count} votes

Accepted answer
  1. GrimOfDoom 101 Reputation points
    2021-12-12T22:32:38.567+00:00
    //Setup local IP\
    string localIP = LocalIPAddress();
    
    app.Urls.Add("http://" + localIP + ":5072");
    app.Urls.Add("https://" + localIP + ":7072");
    
    app.Run();
    
    
    static string LocalIPAddress() {
        using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0)) {
            socket.Connect("8.8.8.8", 65530);
            IPEndPoint? endPoint = socket.LocalEndPoint as IPEndPoint;
            if (endPoint != null) {
                return endPoint.Address.ToString();
            } else {
                return "127.0.0.1";
            }
        }
    }
    

    Somehow through the many, many, many trial and errors of literal head pounding- the issue was not networking at all, but the web builder not knowing to accept data from localIP, which I had to find & build a function to "most accurately" get. Now Asp.Net 6 minimal API can be found on other devices easily.

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2021-12-12T18:06:00.527+00:00

    If it works locally and the other computers can ping the hosting server, then the hosting machine needs firewall rules to open the port used by the core app. This is same if using node.


  2. AgaveJoe 30,126 Reputation points
    2021-12-12T22:37:21.247+00:00

    launchsettings.json only works for development. Configure kestrel to listen on an IP and port. Below is a basic .NET 6 example (for educational purposes) using the standard Web API template.

    var builder = WebApplication.CreateBuilder(args);
    
    builder.WebHost.ConfigureKestrel(options => options.Listen(System.Net.IPAddress.Parse("192.168.86.245"), 5003));
    // Add services to the container.
    
    builder.Services.AddControllers();
    // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
    builder.Services.AddEndpointsApiExplorer();
    builder.Services.AddSwaggerGen();
    
    var app = builder.Build();
    
    // Configure the HTTP request pipeline.
    if (app.Environment.IsDevelopment())
    {
        app.UseSwagger();
        app.UseSwaggerUI();
    }
    
    //app.UseHttpsRedirection();
    
    app.UseAuthorization();
    
    app.MapControllers();
    
    app.Run();
    

    The URLs
    http://192.168.86.245:5003/swagger/index.html
    http://192.168.86.245:5003/weatherforecast

    If you want to use TLS then you'll need to create an self-signed certificate. Usually certificates are assigned to a domain like localhost.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.