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.
Related
I am working on an MVC project (.NET Framework 4.7.2) that uses AspNet.Identity to control access to pages. I have some static documentation files generated by TypeDoc that I wanted to be able to control access to. Serving the files proved to be no problem, I just created a wwwroot directory and put the files in there. The files can then be viewed via a URL of the form /wwwroot/index.html (etc.).
However, since these are documenting the code, I wanted to be able to lock this down. I have seen articles on how to do this in .Net Core but have yet to track down how to do things using AspNet.Identity. There is, of course, the laborious way of doing this - i.e. recreating every page as a view with a controller and applying a tag like [Authorize(Roles = "Developer")] to the controller but this would defeat the object of being able to generate these files automatically.
The project has out-of-the box files such as App_Start/Startup.Auth.cs and Controllers/AccountController.cs that include AspNet.Identity code, so I am guessing I may need to put some additional code in one of these.
I also note that serving the static files works despite not adding (or finding) any code like app.UseStaticFiles();, as used in .Net Core.
Any advice/pointers on this would be greatly appreciated!
I have a bit of data stored in a file that I would like to display on a Razor Page in my project. The obvious thing to do would be to just read the file in the .cshtml.cs file, pass it in the model, and display it. That, or caching.
The file, however, won't change often (if at all), and any time it would change, would be accompanied by the code of the app itself changing, so a build would be necessary one way or another.
That's how I got the idea of somehow generating the desired HTML on build time.
I know that, on build time, .cshtml files are compiled into regular C#, and I was looking for a way to, perhaps, hook into this process somehow. Alas, without much success.
My other attempt was a .razor file, rendered in a Static way, with data fetched in ObInitializedAsync() which was supposed to run only once per component. However, I think that hosting this component in a Razor page interfered with it somehow, and the code was running on every page load. That, or I misunderstood the lifecycle in the first place.
I even went as far as making a .cs-only component and manually using the RenderTreeBuilder from an overridden BuildRenderTree, but it also seems to render per page load.
Lastly, I briefly considered using a source generator to create a partial, but alas, source generators can't read files, so that attempt also failed.
I'm, quite frankly, out of ideas at this point. Hence the question, perhaps someone else had the same issue, or knows something I don't.
So I've been making an MVC project but for some reason, some views work and others don't, whenever I try to show certain views I get this error:
It's in Spanish but it says the resource was not found even though the view is there in visualstudio? It's like visualstudio won't recognize certain views at random, or at least it seems random to me. I've checked that all the #Url.Action for every view that won't show up are correct and they all are.
Also on a side note, I deleted all the insert methods in a CRUD in another project and the project STILL inserted into the database even though there were no insert methods at all, again, it's like visualstudio doesn't recognize changes or certain documents stay in an older version or it doesn't recognize new elements added into the project. Any idea what this might be?
Make sure you enter the correct path, for example like this:
return Redirect("http://localhost:44373/Manage/Index/0");
And please pay attention, if your method has a parameter, then you have to enter it with.
I have use two Project under single solution --
PayrollMVC is Name of Solution which contains two Project
1) Payroll
2) Employee
when i Login from Payroll and Redirect to Employee Project Controller action than it does not Call Controller Action and Layout of Employee Project ..Please
Suggest me How to Map Two project and how to Call Action from one Project to Another and
also I Please Tell me What is the issue of not Calling Action and Layout of Employee Project
One solution to call an action from one solution to the other would be to use Server Transfer in the controller like so:
public ActionResult Whatever()
{
string url = //...
Request.RequestContext.HttpContext.Server.TransferRequest(url);
return Content("success");//Doesn't actually get returned
}
Copied from https://stackoverflow.com/a/20913290/201648
This is useful if you want to quickly transfer a handful of controllers to a page on another website using the same URL, e.g. http://example.com/payroll/foo could show the content http://example.com/employee/foo but still appear at http://example.com/payroll/foo. This solution is quick to implement but also hacky, and it doesn't solve you layout issues (please continue reading about areas below for a complete solution).
In terms of not calling the actions and layout, it's because they're in a different project (so the current project doesn't know about them). For this reason you'd probably want to go to the trouble of setting up areas. There is a full guide for areas here:
https://msdn.microsoft.com/en-us/library/ee671793%28v=vs.100%29.aspx
You can setup shared layouts for areas as described in this link:
http://brockallen.com/2012/08/31/sharing-a-single-_viewstart-across-areas-in-asp-net-mvc/
All the routes (for the controllers) can be configured centrally. I'd start by looking at Registering Area Routes and Linking Between Areas from the first link I posted.
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.