编辑:我现在已将Request
更改为Context.Request
,现在收到了另一个错误信息,与此帖子的标题不同:Cannot apply indexing with [] to an expression of type 'HttpRequest'
所以我正在尝试介绍ASP.Net,并按照这里提供的在线教程进行操作:https://www.w3schools.com/asp/webpages_forms.asp(“显示图像”部分)。我正在尝试以MVC风格的布局来实现它。 结构的起始模板是运行dotnet new -t web
时生成的模板的修改版本。
在我的Pictures.cshtml文件中,我有以下代码:
@{
ViewData["Title"] = "Pictures";
var imagePath="";
if (Request["Choice"] != null)
{
imagePath="images/" + Request["Choice"];
}
}
<h2>Pictures</h2>
<form method="post" action="">
I want to see:
<select name="Choice">
<option value="Photo1.jpg">Photo 1</option>
<option value="Photo2.jpg">Photo 2</option>
<option value="Photo3.jpg">Photo 3</option>
</select>
<input type="submit" value="Submit" />
@if (imagePath != "")
{
<p>
<img src="@imagePath" alt="Sample" />
</p>
}
</form>
这是从MainController.cs中调用的:
using Microsoft.AspNetCore.Mvc;
namespace WebApp.Controllers
{
public class MainController : Controller
{
public IActionResult Pictures()
{
return View();
}
}
}
还有一个_ViewStart.cshtml文件引用了_Layout.cshtml。
运行时,我被重定向到错误页面,并在终端上收到错误消息The name 'Request' does not exist in the current context
。
请问有人能指导我缺少什么或者我做错了什么吗?为什么这个教程示例在我的项目中无法工作?
谢谢 :)