“‘<' is an invalid start of a value (Blazor)"

huangapple go评论73阅读模式
英文:

'<' is an invalid start of a value (Blazor)

问题

在我的Create.razor页面中,我设置了URL,创建了一个新的博客帖子,并导航到它的页面:

```csharp
async Task CreateNewBlogPost()
{
    newBlogPost.Url = $"{newBlogPost.Chars}/{newBlogPost.Title.Replace(" ", "-")}";
    var result = await BlogService.CreateNewBlogPost(newBlogPost);
    NavigationManager.NavigateTo($"{newBlogPost.Chars}/{newBlogPost.Title.Replace(" ", "-")}");
}

这个razor页面打开的是:@page "/{char}/{title}"
在其中运行了这段代码:

[Parameter]
public string Char { get; set; }

[Parameter]
public string Title { get; set; }

protected override async Task OnParametersSetAsync()
{
    string url = $"/{Char}/{Title}";
    CurrentPost = await BlogService.GetBlogPostByUrl(url);
}

我知道不断重新创建URL有点多余,但过去在不直接使用Char和Title时我曾遇到导航问题。然后它调用了GetBlogPostByUrl(),它是这样的:

public async Task<BlogPost> GetBlogPostByUrl(string url)
{
    //var post = await _http.GetFromJsonAsync<BlogPost>($"api/Blog/{url}");
    //return post;

    var result = await _http.GetAsync($"api/Blog/{url}");
    if (result.StatusCode != System.Net.HttpStatusCode.OK)
    {
        var message = await result.Content.ReadAsStringAsync();
        Console.WriteLine(message);
        return new BlogPost { Title = message };
    }
    else
    {
        return await result.Content.ReadFromJsonAsync<BlogPost>();
    }
}

然而,我接着在控制台中收到这个错误:

Unhandled exception rendering component: '<' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.
System.Text.Json.JsonException: '<' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.

我已经使用断点测试了不同的值,一切都看起来很正常。URL正确,else命令运行,这意味着它是200 OK,而且博客帖子已经创建在SQLite数据库中。我有点困在为什么会出现这个错误。我认为问题出在readfromjsonasync(blogpost),但我不知道该从哪里入手。谢谢。

我的应用程序是一个带有ASP.NET托管的Blazor Web Assembly,因此我拥有客户端、服务器和共享端。如果需要的话,我可以提供我的博客控制器。再次感谢。


<details>
<summary>英文:</summary>

In my Create.razor page where I create a blog, I set the URL, create a new blog post, and navigate to its page:

async Task CreateNewBlogPost()
{
newBlogPost.Url = $"{newBlogPost.Chars}/{newBlogPost.Title.Replace(" ", "-")}";
var result = await BlogService.CreateNewBlogPost(newBlogPost);
NavigationManager.NavigateTo($"{newBlogPost.Chars}/{newBlogPost.Title.Replace(" ", "-")}");
}



This razor page opens: @page &quot;/{char}/{title}&quot;
Within it, it runs this:

[Parameter]
public string Char { get; set; }

[Parameter]
public string Title { get; set; }

protected override async Task OnParametersSetAsync()
{
    string url = $&quot;/{Char}/{Title}&quot;;
    CurrentPost = await BlogService.GetBlogPostByUrl(url);
}

I know it&#39;s a little overkill to constantly be recreating the URL but I&#39;ve been experiencing navigation problems in the past when not using Char and Title directly. It then calls `GetBlogPostByUrl()` which is this:

public async Task<BlogPost> GetBlogPostByUrl(string url)
{
//var post = await _http.GetFromJsonAsync<BlogPost>($"api/Blog/{url}");
//return post;

        var result = await _http.GetAsync($&quot;api/Blog/{url}&quot;);
        if (result.StatusCode != System.Net.HttpStatusCode.OK)
        {
            var message = await result.Content.ReadAsStringAsync();
            Console.WriteLine(message);
            return new BlogPost { Title = message };
        }
        else
        {
            return await result.Content.ReadFromJsonAsync&lt;BlogPost&gt;();
        }
    }
However, I then proceed to get this error in the console: 

&gt; Unhandled exception rendering component: &#39;&lt;&#39; is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.
System.Text.Json.JsonException: &#39;&lt;&#39; is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.



I&#39;ve used breakpoints to test the different values and everything seems to be good. The URL is good, the else command is ran which means it&#39;s 200OK, and the blogpost is created within the SQLite database. I&#39;m a little stuck on why it is giving me this error. I definitely think it&#39;s the `readfromjsonasync(blogpost)` but I don&#39;t know where to tackle it. Thanks.

My app is a Blazor Web Assembly with ASP.NET hosted, so I have all Client, Server, and Shared side. I can give my blog controller if needed. Thanks again.

</details>


# 答案1
**得分**: 3

Sure, here is the translated content:

```plaintext
string url = $"/{Char}/{Title}";

combined with

var result = await _http.GetAsync($"api/Blog/{url}");

Will call an URL with `api/Blog//{Char}/{Title}`

That double __`//`__ is your main problem.
英文:
 string url = $&quot;/{Char}/{Title}&quot;;

combined with

var result = await _http.GetAsync($&quot;api/Blog/{url}&quot;);

Will call an URL with api/Blog//{Char}/{Title}

That double // is your main problem.

huangapple
  • 本文由 发表于 2023年6月29日 21:15:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581436.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定