I am working in an ASP.NET MVC 4 project. I did not create this project, but whomever did, fixed it so that I cannot put the ControllerName/ActionMethod in the navigation bar to navigate to any page/view in the project (including any new view/page that I create). Given that I cannot navigate to new views/pages that I create myself, I am assuming that there must be some sort of global setting to prevent this, but I am new to MVC and can't figure out where to look or what to look for. Any help with this would be greatly appreciated!!!!
I was able to figure this out (not completely, but enough to make it work). I needed to add
[Authorize] above the public class Controller. Hope this helps someone else!
Related
Sorry if this is a stupid question or has been asked before, but I've been looking for an hour and haven't found any answers.
I'm working on a .NET Core MVC web application, and one of the views has subsections that I want to be able to go to directly, without having to have already loaded the view.
If the view is loaded, then href="viewname.html#sectionID" works, but it won't work if I am currently on another view. It also confuses me that my view files are .cshtml, and not plain HTML files, and it still recognizes which section of the view it needs to go to...
I tried using the Anchor Tag Helper and I couldn't figure out how I would do it with that.
Any help is greatly appreciated.
I got it to work the way I wanted it by mix and matching href="ViewName#sectionID" for when I want to go to a specific section in the view, using href only calls for all views except the homepage one and asp-action="ViewName" for the homepage view(index). Also, I defined the routes for the controller actions for every action other than the homepage. This way, the URL is basically always myURL.com/ViewName/, and on the homepage it's just myURL.com/, so an href that specifies the URL will lead to the desired controller action.
This is pretty basic, and it was just my lack of understanding towards how routing worked, but maybe someone else runs into this and finds this answer useful... I do feel pretty dumb though.
I'm so newbie in here as I try to learn this new stuff. But, I have a problem. On asp.net core identity, there's no AccountController (as many blog say it should be modified there), so how can I modify this? I'm so confused with this. Thank you.
The latest version of Identity has a default UI that's included programmatically, via AddDefaultIdentity (which calls AddDefaultUI under the hood). You can use the Identity scaffold to add just the parts you want to modify, which works like an overload: anything not explicitly included in your project falls back to the default UI version.
Right-click on your project in the Solution Explorer, go to "Add" and then "New Scaffolded Item..." On the left of the resulting window, there's a tab for "Identity", which has just one scaffold to choose from, "Identity". Click the "Add" button. This will get you yet another window, where you can now choose which items you want to scaffold into your project.
Be advised that Identity's default UI makes use of Razor Pages, now, so you still won't get an AccountController and such. However, you can simply include pages like Account\Register and Account\Login, which should be the two places you would need to make this change. Feel free to add and modify anything else you like as well.
I am working on MVC and i started learning Umbraco, I didn't get how to bind the umbraco page with mvc controller get method to show the database values. can anyone suggest any url or video?
Thansk...
What you're looking for is Umbraco route hijacking.
You can read about it here.
https://our.umbraco.org/documentation/reference/routing/custom-controllers
It's easiest to demonstrate with an example : let's say you have a Document Type called 'Home'. You can create a custom locally declared controller in your MVC web project called 'HomeController' and ensure that it inherits from Umbraco.Web.Mvc.RenderMvcController and now all pages that are of document type 'Home' will be routed through your custom controller! Pretty easy right :-) OK so let's see how we can extend this concept. In order for you to run some code in your controller you'll need to override the Index Action.
So, basically, you "simply" need to create a controller named after your document type, so for example, a document type with the name "TextPage" would need a controller called "TextPageController". Now, if you read through the documentation, you'll find that your "TextPageController" will need to inherit from the RenderMvcController. Here's an example how to achieve this.
public class TextPageController : RenderMvcController
{
public ActionResult Index()
{
return View("~/Views/TextPage.cshtml");
}
}
This forum link may help you:
https://our.umbraco.org/forum/developers/razor/38242-Umbraco-MVC4111-Surface-controller-using-an-AJAX-form
We had created a Custom Plugin , The Nop.Web Site provided to us by our Client did not had Controller, models, Nop.Web.cs project. Initially We did not require to access much of the Nop.Web project as everything was managed through Plugin
But now there was need to add a custom Link on the ProductTemplate.Simple.cshtml view of Nop.Web/Themes/Motion/View/Catalog
Link was to add the product into a custom registry Cart.
The link access ActionResult from "Catalog" Controller supposed to be in Nop.Web Controller.
I need to pass it our Custom Plugin Controller and Action Result. But it throws error as it cannot find the controller nor "Our Custom Plugin Controller" neither the "Catalog" controller.
Can anyone suggest us solution to achieve this in better way.
Any help will be appreciated.
Can you open your controller action in a separate window? Are you sure it's valid?
Have you looked at widgets which are supported by nopCommerce? This way you won't need to edit the cshtml file
Today is my first day working with MVC and I am trying to convert my existing Web Forms website into an MVC 4 site.
I have done some reading and am starting to understand how things work but one thing I can not figure out is for the new Layouts (replacing MasterPages) where is the equivalent to the codebehind file? In my current site I have a Master Page that defines the general look and feel but also runs some code in the codebehind to changes a few things dynamically (for localization and DB generated menu system).
So now that I am using MVC and Layouts I can not figure out where I would code all that at, can anyone please point me in the right direction?
(I know MVC does not have code behinds it uses controllers for it.)
As you Know MVC is three layer architecture.
Model
View
Controller
Model is the data entities. You need to store, or show the data.
Views are the html or presentation layer which would be rendered to users.
Controller are the code behind file all of your code would go in controller. It gets data from Models and apply business logic and then pass to views to show or get updated data from view and pass to models and then save to database.
_layout.cshtml file is present at path of ~/Views/Shared/_Layout.cshtml. It is master-page in mvc. You would see your partial-views contains
Layout = "~/Views/Shared/_Layout.cshtml";
this line at top of page. You can change master-page for any views and you can have multiple Layouts.
Layout contains many partial-views like left-navigation, Top-Navigation and content. each of which can be customized from controller.
Here are some links might help you:
MVC Tutorials
Introduction to MVC
Create a Base Controller class and make all your controllers inherit from it.
The MVC equivalent of WebForms' Master Page codebehind is then this Base Controller, where you can put code you need for multiple controllers.
How can I execute common code for every request?
You can't find any examples of what you're trying to do, because that's not how it's done in MVC. There is no equivalent to code behinds.
You're "trying to do" the wrong thing. MVC layouts are simply template files. They have no code behind, and they should have no functionality besides simple display logic.
MVC is a different paradigm from WebForms. You don't use have server-side controls like WebForms. Therefore the idea that you have content in the layout that does it's own thing violates the MVC principles.
You're basically stuck in what's known as the XY problem. That's where you are trying to achieve certain functionality X, and you believe to do that you need to do Y, so all you ask about is Y... when X is what you really need to be asking about.
Please explain the actual thing you are trying to do, and don't assume that it must be done in the way you've always done it. For instance, if you want to localize something, then ask how to localize something. If you want dynamic content somewhere, ask how to do that, but you need to be more specific about these individual problems, and not just gloss over them as you have done here.