英文:
'<' 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 "/{char}/{title}"
Within it, it runs this:
[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);
}
I know it's a little overkill to constantly be recreating the URL but I'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($"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>();
}
}
However, I then proceed to get this error in the console:
> 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.
I'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's 200OK, and the blogpost is created within the SQLite database. I'm a little stuck on why it is giving me this error. I definitely think it's the `readfromjsonasync(blogpost)` but I don'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 = $"/{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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论