0

I'm trying to learn Razor / ASP.NET. I have some sample code that I'm trying to get to run. I can't seem to figure out what references to add.

I get errors on: IsPost below

@{
ViewData["Title"] = "Home Page";
@using Newtonsoft.Json;
@using System.Net;
@using System.IO;

List<string> files = new List<string>();

if (IsPost)
{
    string GetURL = "http://demo.azurewebsites.net/api/File";
    WebClient client = new WebClient();
    Stream dataStream = client.OpenRead(GetURL);
    StreamReader reader = new StreamReader(dataStream);
    var results = JsonConvert.DeserializeObject<dynamic>(reader.ReadLine());
    reader.Close();

    foreach (var item in results)
    {
        files.Add((string)item.filename);
    }
}

}

Adoyt
  • 395
  • 2
  • 4
  • 17
  • 1
    Are you using Visual Studio? If you hover over those, it will suggest the namespaces to import. That doesn't add references, just brings the names in scope. If you need to find out what assembly things are in, generally doing a web search for "c# " will get you the MSDN page describing what assembly it's in. – mason Feb 09 '16 at 03:29
  • Yes, I'm in visual studio. I agree. I'm worked with C# before but for some reason this doesn't work with the .cshtml files. I did a search though like you suggested and did find some of the assemblies. I still can't find what to call to be able to use IsPost. – Adoyt Feb 09 '16 at 03:45
  • You shouldn't have that code in Razor. The View is not the correct place to make a request to a 3rd party API. That should happen in some sort of abstracted layer, called by your Controller. The resulting information should be placed in a Model that's passed to the View. – mason Feb 09 '16 at 03:50
  • 1
    Thanks for the input. I'm just trying to get familiar with Razor. I haven't messed with MVC yet. – Adoyt Feb 09 '16 at 04:01

2 Answers2

0

It is possible that you are typing this in a view without enclosing it in @{...}. In your view, you should have it enclosed in @{...} to tell Razor the code block is a C# (or VB, whichever is the case for your project).

Having said that, inserting too much code in the View is not ideal. Views should only be responsible in presenting data, not in extracting them.

UPDATE

Your issue is likely a duplicate of this SO item: Razor-based view doesn't see referenced assemblies See the answers by @qes and @V.B.

Community
  • 1
  • 1
Frank Fajardo
  • 7,034
  • 1
  • 29
  • 47
  • Yes, I've updated the post with the full code. I can't get IsPost to work. If found that it's a property of the page. It's used in this example: http://www.asp.net/web-pages/overview/getting-started/introducing-razor-syntax-c – Adoyt Feb 09 '16 at 03:57
0

Well, based on the link you provided in your comment in a prior answer, you're using ASP.Net WebPages - which is a great lightweight way of getting an ASP.Net site up using Razor syntax. It's not however MVC so for one thing you won't have things like ViewData, but that's ok - you can use Page or PageData.

This would be how an entire page would look like (though typically you'd use _layout file in combination with "content files"):

@using Newtonsoft.Json;
@using System.Net;
@using System.IO;

@{
    /*
    Page or PageData instead of ViewBag/ViewData
    */    
    Page.Title = "Hello World"; //this is typically used with a _layout.cshtml where the<title> Tag would be

    //You can create/name Page properties/data as needed
    Page.Whatever = "whatever I want";
    Page.H1TagForSeo = "this is the h1 tag";
    Page.SomeInteger = 100;
    Page["MyPageData"] = DateTime.UtcNow;


    List<string> files = new List<string>();

    if (IsPost)
    {
        //IsPost test - this will only run if this page is requested via POST
        for (int i = 0; i < 10; i++)
        {
            files.Add(i.ToString());
        }
    }
}
<!DOCTYPE html>
<html>

<head>
    <title>@Page.Title</title>
</head>
<body>
    <h1>@Page.H1TagForSeo</h1>
    <p>The time is @Page["MyPageData"]</p>
    <p>
        You can use <code>@@Page propertes/data similar to ViewBag/ViewData</code> @Page.Whatever was viewed @Page.SomeInteger times
    </p>

    @if (IsPost)
    {
        <div>Post Test</div>
        <p>This entire section will only be displayed when requested via POST</p>
        <p>@string.Join(",", files)</p>
    }
</body>
</html>

Hth...

EdSF
  • 11,753
  • 6
  • 42
  • 83
  • Thanks for the clarification EdSF. I still can't get IsPost to work. Looks like I must be creating the wrong project type in VS. – Adoyt Feb 09 '16 at 14:48
  • @Adoyt Perhaps. `WebPages` are created by `Add New Web Site` (and then select ASP.net Razor vX). While I _wouldn't_ advise it, you can get `Web Pages` to work in an `MVC Application` as well..with certain "rules of the road" - e.g. avoid (in fact, _don't_ use Razor pages with the same/existing `View` names - like "index" or "about" (which are common scaffolded MVC Views). – EdSF Feb 09 '16 at 15:19
  • @Adoyt is it at least _building_ and/or VS doesn't complain about errors (no red squigglies). Also tried, that code you have as is, and that azure demo site is `403` - that's why I changed it to a trivial sample. When you say "doens't work" - do you mean not hit/evaluated when you debug? – EdSF Feb 09 '16 at 15:21