c# - 加载正确的页面以运行 C# MVC
问题描述
我对 Visual Studio 和 C# 完全陌生。我创建了一个带有硬编码值的测试应用程序以传递给我的搜索查询,并且能够使用 VS 中的 IIS Express 在网页上显示数据。现在我编写了新代码,它将接受用户输入并使用返回结果查询输入。找不到我的本地主机。我不确定我是否正确设置了 Startup.cs。它到达 UseEndpoints 部分并死亡。
这是我的文件:
namespace MyMusicApp
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Register Forecast repository
services.AddScoped<IArtistRepository, ArtistRepository>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Shared/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=MyMusicApp}/{action=SearchArtist}/{id?}");
});
}
}
}
这是我的文件的一些层次结构:
- 控制器:MusicAppController.cs
- 浏览量:ArtistInformation(文件夹)
- ArtistInformation.cshtml(文件)
- SearchArtist.cshtml(文件)
我有从我的 JSON 响应中填充的模型(.cs),我有帮助显示的模型(.cs)(我认为)
以下是填充视图的模型:
public class ArtistInformation
{
[Display(Name = "Artist:")]
public string name { get; set; }
[Display(Name = "Title:")]
public string title { get; set; }
[Display(Name = "Album:")]
public string album { get; set; }
}
public class SearchArtist
{
// Annotations required to validate input data in our model.
[Required(ErrorMessage = "You must enter an artist name!")]
[RegularExpression("^[A-Za-z ]+$", ErrorMessage = "Only text allowed")]
[Display(Name = "Artist Name")]
public string ArtistName { get; set; }
}
以下是观点:
@model MyMusicApp.MusicAppModels.SearchArtist
@{
ViewData["Title"] = "SearchArtist";
}
<div class="container">
<form method="post">
<div class="container">
<div class="form-group col-md-offset-3 col-md-5">
<h2>Search the forecast in a city</h2>
<label asp-for="ArtistName" style="font-family: Arial"></label>
<input asp-for="ArtistName" class="form-control" />
<span asp-validation-for="ArtistName" class="text-danger"></span>
</div>
</div>
<div class="container">
<div class="form-group col-md-offset-3 col-md-5">
<button asp-controller="MusicAppController" asp-action="SearchArtistSongTitles" class="btn btn-primary">Search</button>
</div>
</div>
</form>
</div>
艺术家信息视图:
@{
ViewData["Title"] = "ArtistInformation";
}
<h1>ArtistInformation</h1>
<div class="container">
<div class="container">
<div class="form-group col-md-offset-3 col-md-5">
<h4>Artist Song Title and Album Information for @Model.data[1].artist.name</h4>
<hr />
<img src=@Model.data[1].artist.picture_big style="width:32px" />
@foreach (var var in Model.data)
{
<div>
<div>Song title: @var.title</div>
<div>Album: @var.album.title</div>
<img src=@var.album.cover_xl style="width:32px" />
<hr />
</div>
}
</div>
</div>
<div class="container">
<div class="form-group col-md-offset-3 col-md-5">
<form method="get">
<button asp-controller="MusicAppController" asp-action="SearchArtistSongTitles" class="btn btn-success">Return</button>
</form>
</div>
</div>
</div>
就像我说的,当我使用创建初始项目时自动生成的 index.cshtml 显示响应时,我的代码可以正常工作。现在我添加了更多。
任何帮助开始显示搜索页面,然后在搜索时显示 ArtistInformation。
解决方案
当您在其中添加地图模式时,Startup.cs
我认为您的控制器名称错误。您的控制器已命名MusicAppController.cs
,但您已将控制器映射路由模式设置为
pattern: "{controller=MyMusicApp}/{action=SearchArtist}/{id?}");
尝试将其更改为
pattern: "{controller=MusicApp}/{action=SearchArtist}/{id?}");
另外,我假设您的控制器中有一个名为SearchArtist
推荐阅读
- swift - 快速处理 Twilio VoIP 呼叫代表
- php - symfony中ckeditor的默认模板
- android - Vimeo 视频全屏按钮未在 android webview 中显示。你能帮我么?
- bash - 如何通过 dockerfile 升级 bash?
- php - CentOS 7 无法安装 php-imap 扩展
- ios - 致命错误:在展开 Optional 值时意外发现 nil。iOS 天气警报应用
- node.js - Cheerio 等不及节点获取
- r - 如何包含响应式输出文本 Shiny
- excel-formula - 想要 Table2 中的所有值针对 Table1 中的每个条目
- docker - Docker无法重新启动Windows 10