skip to Main Content

Our institution will only allow us one domain name (foo.bar.edu), so all of our websites are forced to be like this https://foo.bar.edu/Website1

I have a .NET Core 3.1 project, I am trying to get it to start on that site.

If I add this to the Configure, it will work, but also the default path will work.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UsePathBase("/Website1");

so now http://localhost:5000/Website1 works, but also http://localhost:5000 works too.

How can I disable http://localhost:5000 from working? Only paths that have /Website1 should work.

The real reason for this is to test production easily since production must be foo.bar.edu/Website1/route/Index

2

Answers


  1. According to your description, if you want to disable the default path, I suggest you could consider using custom middleware.

    You could build a custom middleware to disable all the path which is not start with the Website1.

    More details, you could refer to below codes example:

                 app.Use((context, next) =>
                {
                    string s = context.Request.Path;
                  
     (!context.Request.Path.StartsWithSegments("/Website1"))
                    {
                      
                        context.Response.StatusCode = 404;
                        return Task.CompletedTask;
                    }
    
                    return next();
                });
                app.UsePathBase("/Website1");
    

    Also you could consider using the asp.net core url rewrite middleware to redirect from / to /Website1.

    More details about how to use url rewrite middleware, you could refer to this article.

    Login or Signup to reply.
  2. Try something like below :

    Public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        
    
        app.UseRouting();
    
        app.Map("/api", ConfigureApiRoutes); // Add a prefix to all routes
    
    
        
    
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers(); // Other middleware configuration
            // Other endpoint mappings
        });
    }
    
    private static void ConfigureApiRoutes(IApplicationBuilder app)
    {
        app.UseEndpoints(endpoints =>
        {
            // Configure routes specific to the "/api" prefix
            endpoints.MapControllerRoute(
                name: "api_default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
            
            // Other endpoint mappings specific to the "/api" prefix
        });
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search