"Index was outside the bounds of the array" error while publishing a .NET Web application

Viewed 1721

I have a simple .NET Web application that works fine when I run it on my local machine. However, when I try to publish the application to a local folder, I see the error that says "Index was outside the bounds of the array." This error, I'm afraid, is very vague and I am not able to figure out what the issue is.

I am creating this project on Visual Studio community 2019.

Can someone please help me figure out what the issue is :)

2 Answers

I do not know the issue or solution for this, but here is a workaround I used. Essentially, you will need to create the publish folder and file profiles manually.

I did this for a .NET Core 3.1 Blazor project

Step 1 - In your project, go to the Properties folder where you should find your launchSettings.json

Step 2 - Inside, create a folder and name it "PublishProfiles"

Step 3 - Inside of the PublishProfiles folder, create a file and name it "FolderProfile.pubxml"

Step 4 - Inside of this file, paste this code and add the location path to the folder you which to publish:

<?xml version="1.0" encoding="utf-8"?>
<!-- https://go.microsoft.com/fwlink/?LinkID=208121. 
    -->
<Project ToolsVersion="4.0"
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <DeleteExistingFiles>True</DeleteExistingFiles>
        <ExcludeApp_Data>False</ExcludeApp_Data>
        <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
        <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
        <LastUsedPlatform>Any CPU</LastUsedPlatform>
        <PublishProvider>FileSystem</PublishProvider>
        <PublishUrl>Your location path where you want to publish your application</PublishUrl>
        <WebPublishMethod>FileSystem</WebPublishMethod>
        <SiteUrlToLaunchAfterPublish />
        <TargetFramework>netcoreapp3.1</TargetFramework>
        <ProjectGuid>08d93ba4-c83c-47e9-a83e-73f12d97626a</ProjectGuid>
        <SelfContained>false</SelfContained>
    </PropertyGroup>
</Project>

Step 5 - Create another file and name it "FolderProfile.pubxml.user" and paste this code inside. Again, insert the path to your desired folder:

<?xml version="1.0" encoding="utf-8"?>
<!-- https://go.microsoft.com/fwlink/?LinkID=208121. 
    -->
<Project ToolsVersion="4.0"
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <_PublishTargetUrl>Your location path where you want to publish your application</_PublishTargetUrl>
        <History>True|2021-07-02T16:47:41.7255934Z;True|2021-07-02T12:44:24.1064885-04:00;True|2021-05-21T09:22:02.2383330-04:00;True|2021-05-20T17:42:40.0758569-04:00;True|2021-05-20T09:45:45.4390896-04:00;True|2021-05-20T09:02:32.8340404-04:00;</History>
    </PropertyGroup>
</Project>

Step 6 - After this, you should be able to right click your project in Visual Studio, click the option Publish... and the screen with the Publish button should be visible.

It's just a workaround but works. Use Release instead of Debug in Solution Configuration options at the top of Visual Studio and copy the generated file from "bin\Release" folder to your destination "Publish" folder.

Related