gpt4 book ai didi

c# - 我可以在同一个 Web 应用程序中有 2 个(或更多)不同的 ASP.NET Core 编程模型吗?

转载 作者:行者123 更新时间:2023-11-30 17:25:40 25 4
gpt4 key购买 nike

我正在使用 Visual Studio 2019 Community16.3.0 Preview 4.0、.NET Core 3.0 RC1、ASP.NET Core 3、Entity Framework 3 RC1

我正在阅读 ASP.NET Core 文档。我知道 ASP.NET Core 有 4 种编程模型:

第一个问题:我想知道,我可以在同一个网络应用程序中有 2 个不同的 ASP.NET Core 编程模型吗?

第二个问题:我按照本教程创建了一个 Blazor 网络应用程序:https://docs.devexpress.com/Blazor/401057/getting-started/create-a-new-application , 运行成功。

Blazor

@page "/weather"
<h1>Weather</h1>
<DxTextBox></DxTextBox>

@code {
//...
}

我尝试将 ASP.NET Core Razor 页面编程模型放入现有的 Blazor 网络应用程序创建.cshmtml

@page "/Create"
@model RazorPagesMovie.Pages.CreateModel

@{
ViewData["Title"] = "Create";
}

<h1>Create</h1>

<h4>Movie</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Movie.Title" class="control-label"></label>
<input asp-for="Movie.Title" class="form-control" />
<span asp-validation-for="Movie.Title" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Movie.ReleaseDate" class="control-label"></label>
<input asp-for="Movie.ReleaseDate" class="form-control" />
<span asp-validation-for="Movie.ReleaseDate" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Movie.Genre" class="control-label"></label>
<input asp-for="Movie.Genre" class="form-control" />
<span asp-validation-for="Movie.Genre" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Movie.Price" class="control-label"></label>
<input asp-for="Movie.Price" class="form-control" />
<span asp-validation-for="Movie.Price" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>

<div>
<a asp-page="Index">Back to List</a>
</div>

@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

创建.cshmt.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using acc133blazor.Models;

namespace RazorPagesMovie.Pages
{
public class CreateModel : PageModel
{
private readonly foo.Models.fooContext _context;

public CreateModel(foo.Models.fooContext context)
{
_context = context;
}

public IActionResult OnGet()
{
return Page();
}

[BindProperty]
public Movie Movie { get; set; }

// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://aka.ms/RazorPagesCRUD.
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}

_context.Movie.Add(Movie);
await _context.SaveChangesAsync();

return RedirectToPage("./Index");
}
}
}

我不确定它能否运行成功。我尝试调试网络应用程序,转到 Razor 页面 https://localhost:44303/Create 和错误。

但它的 Blazor 组件集仍然不够(在 beta 中,只有 12 个组件 https://demos.devexpress.com/blazor/ ),我尝试添加 ASP.NET Razor 页面,或 ASP.NET Core 控件(ASP.NET Core ViewComponent https://demos.devexpress.com/ASPNetCore/ ) , 我可以这样做吗?

最佳答案

不使用Blazor组合在一起是没有问题的。

对于 Blazor,虽然您可以将它与 Razor Pages 结合使用,但是 asp-pageasp-for 标签助手将不再起作用。

引用Blazor Component Embedded in Razor Page

Link to a page with a Blazor component and all of the other page links will no longer function. You can even replicate the same situation using the boilerplate template app for a Blazor Server Side app and linking to any mvc view or razor page. All links no longer work. This has to be a Startup issue. Removing endpoints.MapBlazorHub(); will allow the links to work again but obviously that breaks the Blazor component.

对于部分 View / View 组件,它们在 Blazor 中也不起作用,它们可以更改为 Razor 组件。

引用Can you use a ViewComponent in a Blazor layout

https://github.com/aspnet/Blazor/issues/1066

关于c# - 我可以在同一个 Web 应用程序中有 2 个(或更多)不同的 ASP.NET Core 编程模型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58041302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com