jamessct
jamessct

Reputation: 991

"The name 'Request' does not exist in the current context"

EDIT: I have now changed Request to Context.Request and am receiving a different error to the title of this post now: Cannot apply indexing with [] to an expression of type 'HttpRequest'

So I am trying to introduce myself to ASP.Net and am following an online tutorial available here: https://www.w3schools.com/asp/webpages_forms.asp (the 'Displaying Images' part). I am attempting to implement this in an MVC style layout. The starting template for the structure is a modified version of the template that is produced when you run dotnet new -t web.

In my Pictures.cshtml file, I have the following:

@{
    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>

This is called from a MainController.cs as so:

using Microsoft.AspNetCore.Mvc;

namespace WebApp.Controllers
{
    public class MainController : Controller
    {
        public IActionResult Pictures()
        {
             return View();
        }
    }
}

There is also a _ViewStart.cshtml which references a _Layout.cshtml.

When running this, I am redirected to my error page, and the terminal gives the error The name 'Request' does not exist in the current context

Can somebody help point me in the right direction as to what I am missing or what I have done wrong? Why does this tutorial example not work in the context of my project?

Cheers :)

Upvotes: 6

Views: 12508

Answers (2)

fiverbox.com
fiverbox.com

Reputation: 125

I use this in ASP Core 5.0 :

ViewContext.HttpContext.Request.Query["Choice"]

example in JS:

if (getParameterByName("error") != null) {
   window.location.href = "@Url.Action("Login", "Home" , new { error = ViewContext.HttpContext.Request.Query["Choice"] })";
}

Upvotes: 4

Sum None
Sum None

Reputation: 2279

I had the same error in Core 2.1. I've found if you put it in the ViewData or Styles section, it's hard to debug (or something just wonky about it). Otherwise, it should work like this for a specific parameter:

@Context.Request.Query["Choice"]

For your code, I would just request the value once and assign it to a variable, then see if it's empty or not. Then, create the path from your variable:

var choice = @Context.Request.Query["Choice"];
var imagePath="";
if (!String.IsNullOrEmpty(choice)){
   imagePath="images/" + choice;
}

As a bonus, you can also get the entire query string with all the names and values (in case you want to parse it or whatever) by going:

@Context.Request.QueryString

Upvotes: 6

Related Questions