首页 > 解决方案 > 无法在 VS 2019 中调试方法,

问题描述

我无法调试我的项目的客户端。每次我在方法中的任何位置设置断点,当我从 UI 输入方法时,它都会带我回到 VS 并告诉我Unable to retrieve source content (Unable to retrieve source content)。我在整个互联网上查看了是否有人在调试时在 blazor web 程序集方面有过这种经验并且无法找到解决方案。我什至尝试tools -> options -> debugging从下图中取消选中蓝色箭头并尝试再次调试,但它并没有解决我的问题。我尝试检查下图中的红色箭头,但仍然没有解决我的问题。我不知道为什么它突然停止让我调试并返回这些错误。当我检查页面时,它显示了我Could not load content for https://localhost:5001/FileReaderComponent.js.map. 请看下面的图片。我将附上我的客户端项目,以便您了解我所拥有的。有没有人有一个可以让我调试的解决方案?

调试设置

无法检索源内容

.exe错误

断点设置

检查控制台消息

launchsettings.json

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:27123",
      "sslPort": 44340
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "ClientSide": {
      "commandName": "Project",
      "dotnetRunMessages": "true",
      "launchBrowser": true,
      "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
      "applicationUrl": "https://localhost:5011;http://localhost:5012",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

index.razor

@page "/"

<h1>Upload File!</h1>

<UploadFile />

UploadFile.razor

<div class="row">
    <div class="col-4">
        <div class="form-group">
             <input type="file" @ref="inputReference" @onchange="async () => await OpenFile()" />
             <ul>
                <li>File Name: @fileName</li>
                <li>Size: @size</li>
                <li>Type: @type</li>
             </ul>
        </div>
        <button class="btn btn-block btn-success" @onclick="async () => await UploadFileSelected()">Upload File</button>
        @if (!String.IsNullOrWhiteSpace(message))
        {
            <div class="alert alert-success"> File has been sucessfully uploaded!</div>
        }
    </div>
</div>

UploadFile.razor.cs

using Microsoft.AspNetCore.Components;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Tewr.Blazor.FileReader;

namespace ClientSide.Shared
{
    public partial class UploadFile
    {
        [Inject]
        public HttpClient _httpClient { get; set; }
        [Inject]
        public IFileReaderService fileReader { get; set; }
        public ElementReference inputReference { get; set; }
        public String message = String.Empty;
        public String fileName = String.Empty;
        public String type = String.Empty;
        public String size = String.Empty;
        public Stream fileStream;

        public UploadFile() { }

        private async Task OpenFile()
        {
            // Read the files
            var file = (await fileReader.CreateReference(inputReference).EnumerateFilesAsync()).FirstOrDefault();

            if (file == null)
                return;

            // Get the info of the files
            var fileInfo = await file.ReadFileInfoAsync();

            fileName = fileInfo.Name;
            size = $"{fileInfo.Size}b";
            type = fileInfo.Type;

            // memory stream ms, that has all the buffer or byte array of file contents
            using (var ms = await file.CreateMemoryStreamAsync((int)fileInfo.Size))
            {
                fileStream = new MemoryStream(ms.ToArray());
            }
        }

        private async Task<HttpResponseMessage> UploadFileSelected()
        {
            // Create the content
            var content = new MultipartFormDataContent();
            content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");

            content.Add(new StreamContent(fileStream, Convert.ToInt32(fileStream.Length)), "file", fileName);

            string url = "https://localhost:5001"; // this is the web api host url

            var response = await _httpClient.PostAsync($"{url}/api/upload", content);
            response.EnsureSuccessStatusCode();
            var responseContent = await response.Content.ReadAsStringAsync();

            var result = JsonConvert.DeserializeObject<HttpResponseMessage>(responseContent);

            message = "Uploaded!";

            return await Task.FromResult(result);
        }
    }
}

标签: c#asp.netasp.net-core-webapiblazor-server-sideblazor-webassembly

解决方案


推荐阅读