Naming a view "Properties" - c#

This is a .NET MVC 4 website using index for default. So far everything has been fine for this site till I added a new view folder named "Properties", added an index view and created the controller plus a link on the nav bar for it. Everything compiles nicely.
When I run debug, and use the link, I get a 403.14 error trying to browse the directory. If I type the full route with the index it goes to the page correctly. All link and code is as it should be. I have not created any kind of model or form for this page yet.
When I rename the View and controllers to simply "Property" everything works correctly.
Is "Properties" as a keyword to avoid?
Was I asleep that day in class?

I'm assuming you're running into this problem during local development? There's a physical directory called Properties in your project, which contains the AssemblyInfo file. When you're attempting to browse to /Properties, it would first look for a physical location, which it finds, then fail because it can't find a document in it.
If you publish this to a real web location, there won't be a Properties directory, so it should work. Or you can simply delete the directory and then it should work locally.
But personally, I wouldn't recommend that - just pick a different name. It's easier to just try to avoid conflicts like this whenever possible.

Related

ASP.NET Core 6 MVC scaffolded identity override - how to replace pages AFTER scaffolding?

I have an inherited project that has a scaffolded identity. Everything works fine. Now, I would like to change some part of the page (example) Email.cshtml that is supposed to be in the Manage folder. However, at the time of creation, the dev did not override the file Email.cshtml, thus I created a new file with the exact same name.
However, the project is not recognizing the new Email.cshtml file at all - it's like it doesn't exist.
I'm not quite sure what I should do here, and I tried re-scaffolding the entire Identity (which didn't work), also tried copying a brand new scaffolded identity Email.cshtml file from another project (changed namespace etc to fit project), and nothing worked.
How do I make a new file to override Identity's scaffolded files that were deleted? Any help is really appreciated.
First of all, according to the Official Documentation;
Generated code takes precedence over the same code in the Identity RCL (Razor Class Library)
So, you aren't supposed to "create" any class by yourself when you want to customize Identity code.
I went ahead and Scaffolded Identity in ASP.NET Core 7 MVC, and I can see the "Email.cshtml" class under "Areas/Identity/Pages/Account/Manage". Are you sure you are looking at the right location?
I have recreated the Email.cshtml and Email.cshtml.cs from a brand new project (and modified it to suit the current project paths/namespaces etc). I have edited the routing in other places to point it to MY Email.cshtml file that I've placed in another folder for now.
I must first say, after a lot of attempts to fix this, I did not once close VS and open it back again... which might've solved my problem somewhere along the way, but since it is working now with a new path, I will declare this as solved.

OnActionExecuting not executing in referenced MVC project

I have multiple ASP.NET MVC 5 projects who have headers/menus at the top of the page where the html is exactly the same. There are some menu items, logos, links that get pulled from the database. To eliminate these redundant code across projects, I wanted to use a shared project for just the header, compile and then reference it in each application. I used RazorGenerator.Mvc so my view would be compiled in the DLL and I also use the MvcSiteMapprovider for the menu. So far everything works well. In the shared/referenced project I made a view called "CommonHeader.cshtml" and when I add this partial view to my other MVC projects and compile, it's all good.
I'm having some issue with Sessions, though. In the shared project's controller, I have the following function
protected override void OnActionExecuting
...which calls a sproc and loads some stuff like links/logos into TempData which is then shows in my header eg TempData.Peek("fieldname"). When I run the shared project itself, it loads these TempData fields I put into the View just fine. When I rebuild the other application projects that reference this project, these session/tempdata fields seem to be null. I'm not referencing the session variables from the consuming application views, but from the shared project itself so you would think it would work, but it appears that this OnActionExecuting function doesn't get executed when the code being referenced from another project.
Oddly enough the class that is used by my MvcSiteProvider to build the sitemap/menu will load stuff into Sessions just fine still. So I know I assume the problem ins't that a referenced project can't utilize Session. It just seems like the controller in the referenced project doesn't seem to be running the OnActionExecuting code. Does anyone know why? Or how I can get this to work without having to load all my Session into in that class that loads the SiteMap, keeping it inside the home controller?
I never did figure this out, but I think I came up with a better way to handle it. I can just create a Model that calls the stored procedure used to get all this information (logo, links, etc) and return this to my header view. Granted, it will get called every single time a page loads, but if it's only a few fields it's not a big deal.

Cannot access to physical HTML file

I'm migrate my old fashion asp.net MVC application to an angular application.
I've created a subset of .html files that contains templates that will provide the HTML code to build my angular components.
Problem
I want to put this html files inside the folder "Views" that already exists on my application. But when I try to access to .html files I receive the following message:
But if I put the .html files outside this folder I can access directly to them:
Questions
Can you tell me why this is happening?
There is anyway that I can access to html files inside the folder "Views"?
To make a long story short, the Views folder is a special folder that holds templates used by actions that are routed by ASP.NET MVC. Because of this, you cannot use it to hold files that are meant to be used directly.
I would suggest ditching the .NET Framework all together since you are going Angular.
If that is not an option or you would like to retain ASP.NET MVC functionality, then simply use another folder (like you have already done by moving the Templates folder to the root).
Try adding an explicit ignore in your RouteConfig
routes.IgnoreRoute("{file}.html");
That should prevent the default routing from taking effect
To make this work, I suggest you take the html markup in your html file and put it on a .cshtml view, that you will render using a GET action in the home controller for instance (as you would normaly do in the MVC pattern).
The other way to achieve this (which I don't recommend), is to add your html file to a deployable folder (like the js folder for javascript files), and than you can call your file using a direct link, however you will lose any capabilities of the MVC pattern this way.
Hope this helps.
As html files are working in views folder, Can you check the path i.e 'Editor/V2/Templates' after 'Views' folder whether it is correct?

Go To Controller shows "Unable to find a matching controller." error

In VS 2015, for my Web.API project, I installed MVC 5 from nuget via npm.
This installation added references and generated web.config file under Views folder.
I added new Controller with generated CRUD functions. When I right click and go to View on action it goes with no problem. But reverse action does not work. If I navigate to view via browser it works as well. Web app compiles as well.
Is there any way for me to fix this navigation problem in VS? Did I forget to add something?
Following Works
Following gives Error:
P.S: If I create brand new sample MVC 5 app both actions work as expected, this only happens Web.API project where MVC 5 added later on via nuget.
This happened to me once. Delete the view and try adding the view again by right clicking on the controller action method and clicking Add View again. I think it would solve the problem
It happens while the controller or view may be not exactly in the controller folder or view folder respectively. It may contain an additional folder. You can solve this issue through adding a new view and delete the old view.
There appears to be a default shortcut key Ctrl-M then Ctrl-G.
This will automatically switch between the controller and view page. If you're on the controller/action it will switch to the view and vice versa.
My situation is not exactly the same as your (I added a new controller and new view), but I was receiving the same error message. Ensure that the name of your controller matches the name of the folder that the view is in.
For example, if the controller is TodoController.cs ensure that the view is in a folder called Todo. It doesn't seem to matter what the view .cshtml file is called. That's what fixed it for me.
I had this exact scenario when my controller file name did not match the controller class name:
I had a controller class named InvoicesController, but the file name was InvoiceController.cs. After renaming the file name to the class name of the controller the switching back from the view to the controller started working
I just had this and the controller was in the same folder as the Controllers folder. I moved it into the folder and it worked after that.
At least reading this, I found the shortcut key to switch between controller and view :-)

ASP.NET MVC Custom Routing in Sub Folder

I have searched on Google (may be with wrong keyword) and visited the tutorials of ASP.NET/MVC website. But didn't understand the routing of MVC properly.
I have a scenario of the following figure. I want to display the Index.cshtml when the website lunched.
I have changed the RegisterRoutes method of Global.asax.cs file in various ways. The latest code (not working) is below
routes.MapRoute(
"App", // Route name
"App/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
I will be very happy if someone explains the MapRoute method with different example along with answering my problem.
I don't think the problem is with your routes, I believe it is caused by moving your View and Controller folders to the App folder.
MVC use "convention over configuration" for stuff like where the folders are located, I'm guessing it can't find the Views/Controllers in the new folders?
From here:
These folders are included even in an
Empty ASP.NET MVC application because
the ASP.NET MVC framework by default
uses a “convention over configuration”
approach and makes some default
assumptions based on folder naming
conventions. For instance, controllers
look for views in the Views folder by
default without you having to
explicitly specify this in your code.
Sticking with the default conventions
reduces the amount of code you need to
write, and can also make it easier for
other developers to understand your
project.
I'm not saying that it's not possible to have a folder structure like you have, I just dont believe its supported out of the box.
Personally I'd recommend moving back to the default, if you need to keep this structure then I think you may need to implement your own ControllerFactory and ViewEngine (I'm not sure if these are the correct hooks that you would need to change).
Edit:
Are you trying to implement App as an Area?
Edit2:
If you are trying to add App as an area there are some steps you will need to follow, The reason I wasn't sure if this was what you were trying to do is because I thought areas had to exist within an Areas folder.
There are some steps outlines here Including how to register your area if that is what you are trying to do.
ASP.NET MVC 3 has a built in feature for sub folders. That is called Areas (thanks #Morten for let me know the word). But after finishing the MSDN Walkthrough for Areas, I was still unable to run the web application. The error was showing Configuration errors. #Danny also noticed that.
Then I searched and found that, the Web.config files in the sub folders shouldn't contain application specific properties.So, I removed those properties from the Web.config files. Then I was able to run. But there were some runtime errors when I wanted to navigate to my sub folder/area views. The error was "Could not load type ‘System.Web.Mvc.ViewPage<..<>’" when I click on the links for the views of the Areas folder.
Then I again searched and found helpful this post. And then my application successfully run and I can navigate all of the pages.

Categories

Resources