【发布时间】:2018-05-12 06:46:20
【问题描述】:
抱歉标题不清楚,但不知道如何指定,请随时编辑我的问题。
我设法编写了与 Razor Pages 中给定的代码等效的代码:
我的标记:
<a asp-page="./Index" asp-route-id="@Model.InstructorID" asp-route-courseID="@item.CourseID">Select</a>
这会产生以下 HTML:
<a href="/Instructors/2?courseID=1045">Select</a>
第二条标记:
@Html.ActionLink("Select", "OnGetAsync", new { courseID = item.CourseID })
这会产生以下 HTML:
<a href="/Instructors/2?courseID=1045&action=OnGetAsync">Select</a>
我想知道的是:两者之间有什么区别,除了生成的 URL 不同以及 URL 中的不同部分是什么意思?
编辑
这是我的Startup 课程:
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)
{
services.AddDbContext<Data.SchoolContext>(options => options.UseSqlServer(Configuration.GetConnectionString("ContosoUniversityConnectionString")));
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseMvc();
}
}
【问题讨论】:
-
请在应用程序启动时显示您的路由配置代码(全部)。这是可以解释的,因为路由匹配是如何工作的,但前提是您显示路由代码。
-
当您执行
@Html.ActionLink("Select", "OnGetAsync", new { courseID = item.CourseID })时,您是否也尝试过指定控制器?我只看到操作,并且由于您添加了其他参数 courseID,因此在这种情况下,控制器可能成为必需的。但我只是猜测。 -
您没有在那里定义任何路线。根据docs of the UseMvc() overload、
This method only supports attribute routing. To add conventional routes use UseMvc(IApplicationBuilder, Action<IRouteBuilder>)。因此,如果您使用属性路由,请发布您的路由。如果没有,则您没有任何路由,这就是您的 URL 不起作用的原因。见the routing docs。 -
@NightOwl888 谁说任何东西都不起作用?两者都像魅力一样工作:)我只是好奇:)
-
其实,没有。路由的典型行为是使用路径
/Instructors/2/course/1045,因为查询字符串通常不会影响选择哪个操作方法或传入哪些参数。使用查询字符串也可以工作,但action=OnGetAsync参数意味着某些严重错误。如果您使用的是属性路由,请发布路由。如果没有,您至少应该调用UseMvcWithDefaultRoute()(而不是UseMvc())来设置一些基本路由。
标签: c# asp.net-core asp.net-core-tag-helpers