Continued MVC Area 404 error. At wit's end - c#

[EDITED] Code and solution edited as per suggestions from forum members. I've corrected the name of the controller. It was incorrect. The corrected name is shown in the updated solution file snapshot. I'm getting a new error. The new error is shown in the updated snapshot. It's still a 404-typpe error.
As for the "post your routing edits" type of comments, please note the original statement that Visual Studio has done all the wireup for me. It's my understanding that if I'm right-clicking and adding controllers, views, etc. from the IDE that I do not need to go in and do additional wireup behind the scenes. This seems to defeat the whole purpose of an easy-to-implement interface. Please correct me if I misunderstand. Thanks.[END EDIT]
I asked for help with this problem earlier and nobody was able to solve. I've retooled the solution, deleting everything involved and starting over, in hopes it would relieve the problem. I'm still stuck. This is a very simple thing according to all I've read on MVC but I have been dead in the water for over a week. Would appreciate help on this. My retooled code appears below. I've looked at similar errors reported in these forums and found no help.
Please note that all the wireup was done completely by Visual Studio: I have not modified any generated code behind the scenes other than adding an Action method to the controller. In other words, everything was generated by right-clicking in VS and selecting "Add" and following the prompts for views, controllers, etc.
I have a view in one area, "UserAccount", that uses Html.ActionLink() to generate a link to a view in another area, "DocumentUploaderNew". A screen snapshot of the markup is below. When I click the rendered button in IE though, I get an error that seems like a "404" error. The view IS there though.
This has GOT to be an embarrassingly easy answer. I'm ready to eat humble pie. What is it???

That's because your controller is actually named FileUpload, yet you're trying to use the controller name FileUploaderNew (looks like you're getting confused with your action and controller names).
Try this:
#Html.ActionLink("Upload New", "FileUploaderNew", new { controller = "FileUpload", area = "DocumentUploaderNew" }, new { #class = "btn btn-info" })

The reason this is failing is because your view FileUploaderNew.cshtml is at the root of the Views folder and not in a folder itself (either one with the name of the controller or the shared view folder). If you look at the paths that MVC is checking in your error screenshot, you can see it never checks the path ~/Areas/DocumentUploaderNew/Views/FileUploaderNew.cshtml
There are numerous fixes you can do to fix this problem (choose one, not all three).
Create a folder in your Views folder called FileUploaderNew and move your FileUploaderNew.cshtml file into that folder (basically, you create a subfolder in the views folder with the name of a matching controller. The view engine will look for that folder when returning Views from that controller).
Move the FileUploaderNew.cshtml file into the Shared folder inside the Views folder (you can see it in your screenshot).
Specify exactly where your view is when you call Return View():
return View("~/Areas/DocumentUploaderNew/Views/FileUploaderNew.cshtml");

Depending on what your route is, it'll either be
#Html.ActionLink("Upload New", "FileUploaderNew", new { controller = "FileUpload/FileUploaderNew"}, new { #class = "btn btn-info" })
or
#Html.ActionLink("Upload New", "FileUploaderNew", new { controller = "DocumentUploader/Controllers/FileUpload/FileUploaderNew"}, new { #class = "btn btn-info" })
The jist is you have to post to the controller's path (not name) and when you don't it can't find it & you get a 404. In a default MVC file structure, I believe this is automated (controllers in the controllers directory auto map without paths), but that's not your file structure.
Also, I'm answering this never having used areas, not sure if those factor into the equation, but a quick read seems to indicate they're more for locking down paths than finding them.

Related

React Getting Data Attribute from CSHTML parent element

Brand new to React today so apologies in advance.
I have googled but for whatever reason can't seem to find the answer which I know must be out there!
I'm trying to build a TEST component just to learn.
The component is basically going to consist of a header and a number of name value pairs set out in div blocks. So I'm starting with the header and trying to make the component generic by passing in a data attribute.
I have a cshtml page with this node (solution is a .NET Core MVC project in VS2019):
<div id="detailsHeaderText" data-headerText="Details"></div>
I have set up a jsx file which looks like this:
class Header extends React.Component {
render() {
return (
<div className="col-md-12 col-sm-12"><h5>{document.getElementById("detailsHeaderText").getAttribute("data-headerText")}</h5></div>
);
}
}
ReactDOM.render(<Header />, document.getElementById('detailsHeaderText'));
This works perfectly and returns a header with the word "Details" in it.
I now want to make it generic so I can do this elsewhere on the page:
<div class="detailsHeaderText2" data-id="2" data-headerText="Header2"></div>
<div class="detailsHeaderText3" data-id="3" data-headerText="Header3"></div>
<div class="detailsHeaderText4" data-id="4" data-headerText="Header4"></div>
etc
How can I output the header text based on a data-attribute input?
The idea being that I connect the React render output to the element along the lines of this pseudocode: document.getElementById("detailsHeaderText" + data-id)
I've looked at constructors and super(props) but nothing seems to work as most of the examples are to do with handlers and hence access the event target prop.
I've found many links to passing props between components.
But none for passing in data from the parent element on a cshtml page.
An answer or a pointer to a detailed answer on passing variables into React would be most helpful.
Thanks in advance.
So I'm 12 hours further down the line in terms of learning React and Googling.
And solved the problem.
Working code is:
function Header(props) {
return <div className="col-md-12 col-sm-12"><h5>{props.headertext}</h5></div>;
}
let elems = document.getElementsByClassName("headerText");
function renderToElements(toRender, elements, dataset) {
for (var i = 0; i < elements.length; i++) {
let passText = elements[i].dataset[dataset];
let renderEl = React.createElement(toRender, { headertext: passText })
ReactDOM.render(renderEl, elements[i]);
}
}
renderToElements(Header, elems, 'headertext')
Which renders all dom nodes of the following construct:
<div class="headerText" data-headertext="Details"></div>
It may seem like a pointless exercise to some in terms of what it is achieving but hopefully this may help others in grasping some basics as I/they can now build on this to construct more complex components.

AirConsole View Manager is not defined after uploading for testing

I'm currently building a game, when I tried uploading it to AirConsole the game gives me a error in the preview.
Has anyone had this problem before?
"Uncaught ReferenceError: AirConsoleViewManager is not defined".
var airconsole = new AirConsole({ "orientation": "landscape", "device_motion": 10 });
var vm = null;
window.onload = start;
airconsole.onReady = function () {
//THE LINE UNDER CRASHES
vm = new AirConsoleViewManager(airconsole);
};
ReferenceError: AirConsoleViewManager is not defined
at AirConsole.airconsole.onReady (https://storage.googleapis.com/XXX.xxxxxxx.xxxx.cdn.airconsole.com/2018-05-24-16-33-25/controller.html:125:22)
at AirConsole.onPostMessage_ (https://www.airconsole.com/api/airconsole-latest.js:1053:8)
at https://www.airconsole.com/api/airconsole-latest.js:969:8
Thank you very much :)
I assume you got the information about AirConsoleViewManager from here (GitHub).
I can't tell for sure because you haven't linked the entire file, but you need to download the airconsole-view-manager.js and include that in your controller script.
The example from the previously linked GitHub example is kind of weird because it doesn't include the JS file in it. Most likely the reason for this is that they assume (which you should never do as a developer) people know it already.
EDIT: Actually, they do include the file but it's not done in JS file, it's in HTML file:
<script type="text/javascript" src="airconsole-view-manager.js"></script>
I do not know what the problem was, but after cleaning the build and transfering code over to another project, then uploading again it worked!

MVC using VS2017: The name 'Model' does not exist... and other errors when I create a new controller

Hello to everybody at Stack Overflow and please pardon me if I am missing anything here!
I am studying ASP.NET to prepare myself for an internship. To this end, I am following along with a Microsoft Virtual Academy course entitled "Introduction to ASP.NET Core with Visual Studio 2017".
Specifically, I am watching the video "Routing and MVC", and am now at where Scott H. adds a new folder "Tickets" to "Views" (at the 21:20 minute mark). I have been following his instructions exactly up to this point, and the new index.cshtml file was created under Views/Tickets (which I will explain shortly). However, as this screenshot shows, there is a red squiggly line under ViewData with the associated error
The name 'ViewData' does not exist in the current context
The image shows a total of 8 errors, but I have not made any changes to the file. Neither has Scott in the video up to this point and he is not getting any error with ViewData. What's more, he was able to rename View.cshtml to index.cshtml but it does not work for me (hence I used this name when I created my file, and find that renaming to any other string is discarded without any error except those in the error list).
My situation is similar to the one described in this post in that it is already there by default without needing to do anything, so I suspect it could be attributed to how VS2017 has been set up.
UPDATE!!!
I have continued with the video, seeing no runtime error, and am now nearing the end. By 32:20, Scott H. had added this code to the TicketController class:
public IActionResult Index()
{
// GO TO THE DATABASE!
// GET SOME MODELS (STUFF)
var s = new Seat() { Location = "Orchestra" , Price = 300.00 };
return View();
}
public string Index2()
{
return "Hello from Tickets!";
}
Then he modified index.cshtml to reference using #Model as such:
#model WebApplication3.Models.Seat
#{
ViewData["Title"] = "index";
}
<h2>My New Tickets View</h2>
#Model.Location for only #Model.Price!
When I enter the same code into my editor the squiggly lines appear under #Model with the same error as ViewData:
The name 'Model' does not exist in the current context
Executing the code and navigating to the /tix page does not show Orchestra for only 300! on the webpage as shown in the video, but on my side I get this Internal Server Error instead. As before, I have checked my code and it matches what I see in the video.
You are missing to pass the model to View. Please pass the 'Seat' model object as shown below.
public IActionResult Index()
{
// GO TO THE DATABASE!
// GET SOME MODELS (STUFF)
var s = new Seat() { Location = "Orchestra" , Price = 300.00 };
return View(s);
}

Nancy.ViewEngines.ViewNotFoundException: Unable to locate view 'Index'

I know this question was asked month before but was never answered.
My program starts normally but after returning View["Index", blogPost] it could not find the Index.cshtml and raises an exception.
Get["/"] = parameters =>
{
var blogPost = new BlogPost
{
Id = 1,
Title = "Test",
Content = "Lorem ipsum...",
Tags = { "c#", "aspnetmvc", "nancy" }
};
return View["Index", blogPost];
};
Exception:
Nancy.RequestExecutionException: Oh noes! ---> Nancy.ViewEngines.ViewNotFoundException: Unable to locate view 'Index'
Currently available view engine extensions: sshtml,html,htm,cshtml,vbhtml
Locations inspected: views/Home/Index-de-DE,views/Home/Index,Home/Index-de-DE,Home/Index,views/Index-de-DE,views/Index,Index-de-DE,Index`
If the answer giving my Christian doesn't help you like it didn't help me there is a alternative issue/solution. The alternative is to make sure that the file you created is being copied to the output directory at compile time. You can check it under the properties tab like shown bellow
The exception message tells you where Nancy tried to look for the view:
Locations inspected: views/Home/Index-de-DE,views/Home/Index,Home/Index-de-DE,Home/Index,views/Index-de-DE,views/Index,Index-de-DE,Index
The exception also tells you which file extensions Nancy tried to look for:
Currently available view engine extensions: sshtml,html,htm,cshtml,vbhtml
That is Nancy looks for a file in one of the listed locations with one of the listed extensions.
So the question is if your index.cshtml is in one the listed folders. If not you can either move it there or set up a view location convention.
We ran into this error and noticed the .cshtml file it was complaining about was not getting copied to the server. The solution was in Visual Studio, in the file's properties, we changed the Build Action to Content. This forces it to get included in the build artifacts that are deployed to the server. Similarly you could change the Copy to Output Directory to Always as others have mentioned; either works, but I find setting the file as Content a bit more informative.
In your case, I'm guessing you have an Index.cshtml file which you need to set this property on.

Check if ASP.NET WebForms page exists based upon relative URL provided in Query String

Given a query string url of the form "~/folder/page.aspx", is there a way to check if that page exists within the scope of the application?
I'm in a situation where I'm fixing a minor bug where, if a user attempts to log in to the application from a set of publicly accessible application error pages, then they're redirected back to that public error page. I've been asked to have the user be redirected to the main home page if they're logging in to the application from this state.
So far I've fixed the issue by hard coding the paths to the affected pages in a switch statement, checking the querystring against the hardcoded paths. I feel this is hacky and bad, and would love a more dynamic solution, but I can't seem to find one.
Any help would be greatly appreciated.
Edit - Specifically, my preferred solution would simply be to check that the path defined by the query string url (without a priori knowledge of the exact format) leads to a specified folder within the scope of the application.
So, after looking some more, I discovered Server.MapPath. I can use this in conjunction with System.IO.Directory to see if the file is contained within the directory.
string targetUrl = Request.QueryString["redirect"];
string serverUrlPath = Server.MapPath(targetUrl);
string serverDirPath = Server.MapPath("~/ErrorPages");
foreach (string file in Directory.EnumerateFiles(serverDirPath))
{
if (file.Equals(serverUrlPath, StringComparison.OrdinalIgnoreCase))
{
Response.Redirect(Master.ProjectSearchRedirect());
}
}
Response.Redirect(targetUrl);
I was hoping for something a little more refined (even just a Directory.Contains kind of encapsulation).

Categories

Resources