Asp net core 1 0 vs2015 iisexpress changed app url results in css 404

Introduction

ASP.NET is a popular programming language used for building web . It provides a framework for developing dynamic , web services, and web applications. In this article, we will discuss a common issue faced by ASP.NET developers when the application URL and how to solve it.

The Problem

When changing the application URL in ASP.NET Core 1.0 with Studio 2015 and using IIS Express, you may encounter a CSS 404 . This error occurs because the application is to locate the CSS file due to the in the URL.

The

To solve this issue, you need to update the application's base URL in the Startup.cs file. The base URL is used by the application to resolve relative URLs for static such as CSS, JavaScript, and images.


public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    // Other configuration code
    
    app.UseStaticFiles(new StaticFileOptions
    {
        RequestPath = new PathString("/your-new-url"),
        FileProvider = new PhysicalFileProvider(
            Path.Combine(Directory.GetCurrentDirectory(), "wwwroot"))
    });
    
    // Other configuration code
}

In the above code snippet, we are using the UseStaticFiles method to configure the application to serve static files. We set the RequestPath property to the new URL you want to use for your application. Make sure to replace /your-new-url with the desired URL.

By updating the base URL, the application will now correctly resolve the CSS file and other static files using the new URL. This should resolve the CSS 404 error you were experiencing.

Example

Let's consider an example where you have the application URL from http://localhost:5000 to http://localhost:5000/my-app. To update the base URL, you would modify the code as follows:


public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    // Other configuration code
    
    app.UseStaticFiles(new StaticFileOptions
    {
        RequestPath = new PathString("/my-app"),
        FileProvider = new PhysicalFileProvider(
            Path.Combine(Directory.GetCurrentDirectory(), "wwwroot"))
    });
    
    // Other configuration code
}

By making this change, the application will now correctly serve the CSS file and other static files using the new URL http://localhost:5000/my-app.

Conclusion

Changing the application URL in ASP.NET Core 1.0 with Visual Studio 2015 and using IIS Express can result in a CSS 404 error. However, by updating the base URL in the Startup.cs file, you can resolve this issue and ensure that the application correctly serves static files. Remember to replace /your-new-url with the desired URL in the code snippet provided.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents