9 Replies - 282 Views - Last Post: 07 May 2019 - 10:47 AM Rate Topic: -----

#1 UG Cyber   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 621
  • Joined: 24-July 08

dotnet core 2.2 defaulting to static files

Posted 07 May 2019 - 06:09 AM

I have been working on a web project for quite some time and decided to add Swashbuckle to help document the API endpoints. Once i followed the steps (attempted from several sources, including Microsoft), my project now attempts to load localhost:<port>/index.html on load instead of following the default route
routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");



I have since removed Swashbuckle from the NuGet packages, and all references found in startup.cs/.csproj files and still having the same issue. I have also tried removing the UseStaticFiles() and no luck. Below are the Startup.cs, Program.cs and project file. Any help would be great!

Startup.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using CyberRM.Data;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using CyberRM.Models;
using CyberRM.Areas.Identity.Data;
using Microsoft.AspNetCore.HttpOverrides;

namespace CyberRM
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddDbContext<CyberRMContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));
            services.AddDefaultIdentity<CyberRMUser>()
                .AddEntityFrameworkStores<CyberRMContext>();

            services.Configure<IdentityOptions>(options =>
            {
                // Password settings.
                options.Password.RequireDigit = true;
                options.Password.RequireLowercase = true;
                options.Password.RequireNonAlphanumeric = true;
                options.Password.RequireUppercase = true;
                options.Password.RequiredLength = 6;
                options.Password.RequiredUniqueChars = 1;

                // Lockout settings.
                options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
                options.Lockout.MaxFailedAccessAttempts = 5;
                options.Lockout.AllowedForNewUsers = true;

                // User settings.
                options.User.AllowedUserNameCharacters =
                "ab[email protected]+";
                options.User.RequireUniqueEmail = false;
            });
            services.Configure<ForwardedHeadersOptions>(options =>
                    options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
            );
            services.ConfigureApplicationCookie(options =>
            {
                // Cookie settings
                options.Cookie.HttpOnly = true;
                options.ExpireTimeSpan = TimeSpan.FromHours(1);

                options.LoginPath = "/Identity/Account/Login";
                options.AccessDeniedPath = "/Home/Index";
                options.SlidingExpiration = true;
            });

            services.AddMvc()
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseAuthentication();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "vendor default",
                    template: "Company/{companyId:int}/Vendor/{vendorId:int}/{controller=Dashboard}/{action=Index}/{id?}");

                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}


Program.cs
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace CyberRM
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }
}


.csproj
<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
    <UserSecretsId>aspnet-CyberRM-FEAB79BA-3241-4988-BCFC-EC98B37F172D</UserSecretsId>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.0" PrivateAssets="All" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.2.0" />
  </ItemGroup>
  
  <ItemGroup>
      <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.1" />
  </ItemGroup>
  
  <ItemGroup>
    <Content Update="Views\Company\VendorOverview.cshtml">
      <Pack>$(IncludeRazorContentInPack)</Pack>
    </Content>
  </ItemGroup>
  
  <ItemGroup>
    <Folder Include="Controllers\Company\Campaigns\" />
    <Folder Include="Migrations\" />
    <Folder Include="wwwroot\js\Company\Vendors\" />
  </ItemGroup>

</Project>



Is This A Good Question/Topic? 0
  • +

Replies To: dotnet core 2.2 defaulting to static files

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15170
  • View blog
  • Posts: 60,713
  • Joined: 12-June 08

Re: dotnet core 2.2 defaulting to static files

Posted 07 May 2019 - 06:56 AM

Have you tried swapping this
template: "{controller=Home}/{action=Index}/{id?}");

back to
template: "{controller}/{action=Index}/{id?}");

with any effect?

Also make sure to close out of any VS, and check if IIS is running in the icon nest in the bottom right and close that out too.
Was This Post Helpful? 1
  • +
  • -

#3 UG Cyber   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 621
  • Joined: 24-July 08

Re: dotnet core 2.2 defaulting to static files

Posted 07 May 2019 - 07:07 AM

Yes, I have tried switching the default route as you suggested with no success, then went back to the template style.

I have, however, figured out where the problem child is.
I created a new project with the same name, and copied parts of my current project one by one until the issue occurred again, I found out that the problem was located in the "Properties" directory located in the project directory, so I compared the files in both, the only difference (other than unique pid's and port numbers)was located in the file "{projectDirectory}/Properties/PublishProfiles/FolderProfile.pubxml.user". Below are the contents

Old (With Issue)
<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your Web project. You can customize the behavior of this process
by editing this MSBuild file. In order to learn more about this please visit https://go.microsoft.com/fwlink/?LinkID=208121. 
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <TimeStampOfAssociatedLegacyPublishXmlFile />
    <_PublishTargetUrl>D:\My Web Sites\CyberRM</_PublishTargetUrl>
  </PropertyGroup>
</Project>



New (No issue)
<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your Web project. You can customize the behavior of this process
by editing this MSBuild file. In order to learn more about this please visit https://go.microsoft.com/fwlink/?LinkID=208121. 
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <PublishProvider>FileSystem</PublishProvider>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <ProjectGuid>b33b3d54-ce8e-4b48-92ca-c369724506ae</ProjectGuid>
    <publishUrl>D:\My Web Sites\CyberRM</publishUrl>
    <DeleteExistingFiles>False</DeleteExistingFiles>
  </PropertyGroup>
</Project>



Im going to have to learn more about this file as i have never even noticed the Properties directory before. But i would like to know what exactly is causing it to look for "localhost:<port>/index.html"
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15170
  • View blog
  • Posts: 60,713
  • Joined: 12-June 08

Re: dotnet core 2.2 defaulting to static files

Posted 07 May 2019 - 07:15 AM

Odd. Is it happening when you run it locally or only when you publish it and deploy the site?
Was This Post Helpful? 0
  • +
  • -

#5 UG Cyber   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 621
  • Joined: 24-July 08

Re: dotnet core 2.2 defaulting to static files

Posted 07 May 2019 - 07:18 AM

When i run it locally. It has not been published yet.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15170
  • View blog
  • Posts: 60,713
  • Joined: 12-June 08

Re: dotnet core 2.2 defaulting to static files

Posted 07 May 2019 - 07:31 AM

Okay.. then 'PublishProfiles' should take a back seat as it's for publishing only.


Was this in there before you started messing around? Seems odd to have two routes specified.
106                routes.MapRoute(
107                    name: "vendor default",
108                    template: "Company/{companyId:int}/Vendor/{vendorId:int}/{controller=Dashboard}/{action=Index}/{id?}");
109

Was This Post Helpful? 0
  • +
  • -

#7 UG Cyber   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 621
  • Joined: 24-July 08

Re: dotnet core 2.2 defaulting to static files

Posted 07 May 2019 - 07:35 AM

Yes, that was in there from the very start. Ended up not doing what i wanted it to do and just never actually removed it. I did remove it while looking into this and no luck :/
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15170
  • View blog
  • Posts: 60,713
  • Joined: 12-June 08

Re: dotnet core 2.2 defaulting to static files

Posted 07 May 2019 - 07:37 AM

weird.. so your default start page.. is that an index, a home, or what?
Was This Post Helpful? 0
  • +
  • -

#9 UG Cyber   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 621
  • Joined: 24-July 08

Re: dotnet core 2.2 defaulting to static files

Posted 07 May 2019 - 10:17 AM

The default home page is suppose to be
GET HomeController.Index()


but it was searching for the static file
wwwroot/index.html


which doesn't exist

but replacing that file above fixed it for me. Everything is working as it should now. Ill do some research into that directory and file when i get the chance and post my findings here if i figure anything else out.
Was This Post Helpful? 1
  • +
  • -

#10 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15170
  • View blog
  • Posts: 60,713
  • Joined: 12-June 08

Re: dotnet core 2.2 defaulting to static files

Posted 07 May 2019 - 10:47 AM

Yeah.. routing is a magical box.. not a fan of having to go off the beaten path for it.

Kudos on sussing it out.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1