Sitecore Wrong Route in MVC - model-view-controller

I updated sitecore 7.0 to 7.5 and there are some controller rendering items created by MVC3.
When I execute them in 7.5, it shows an error:
Could not create controller: 'Components'. The current route url is: 'api/sitecore/{controller}/{action}'.
`Message: The controller for path '/api/sitecore/Components/Navigation' was not found or does not implement IController.
The name of controller is Components and action is Navigation
I worked perfectly in Sitecore 7.0.
How can I fix this issue?

Yes, try to append "Controller" into your "components" like "componentsController" and you can also disable CommandRoutePrefix at Sitecore.Speak.Mvc.config file in /App_Config/Includes/
Also, if you want to pass value into specific location, you can use static url instead of #Url.Action(...)

Related

Tutorial ASP.NET Core app not seen by localhost

Using MS Visual Studio 2022 on Windows 10 pro to build an ASP.NET Core MVC for the first time. When this tutorial (https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/start-mvc?view=aspnetcore-6.0&tabs=visual-studio) has me run it, Localhost comes up with some stuff relating to the app (its name, a welcome message). The tutorial says to then add "/MvcMovie/" to the URL in the address field. Then, localhost says "No webpage was found for the web address: https://localhost:44379/MvcMovie/".
I'm new at this. Not sure from the tutorial when/if to actually add something to localhost.
Any help will be appreciated.
If you are refering to this:
In the Configure your new project dialog, enter MvcMovie for Project
name. It's important to name the project MvcMovie. Capitalization
needs to match each namespace when code is copied.
The tutorial is saying that your Project's name should be MvcMovie when you create it. Other than that, the tutorial doesn't ask you to go to localhost/mvcMovie. To figure out the routing in an MVC check the next few parts about Controllers-Views-Models. There will be explained about the routing part and how you can access different parts of the URL.
The MvcMovie mentioned in the document does not require you to enter in the URl, it is just the name of the created project:
If you need to use MvcMovie as the routing address in the Url, you can do this:
Controller:
[ApiController]
public class TestController : Controller
{
[HttpGet]
[Route("MvcMovie")]
public IActionResult MvcMovie()
{
return View();
}
}
View demo:
<h1>This is MvcMovie Controller!</h1>
Test url:https://localhost:7040/MvcMovie
Result:
I think you need to know the routing rules in mvc, you can read this document:
Routing to controller actions in ASP.NET Core

Area routing in ASP.NET Core 5.0

Using ASP.NET Core 5.0 MVC, the application is using an area named Counselor with a HomeController and Index action.
One link is added in Layout.cshtml to counselor.
Also, the endpoint is already added the endpoint in Startup.cs
The problem:
The link created is wrong (localhost:44308/?area=Counselor) but it should be localhost:44308/Counselor/Home/Index
Since I had this problem today too, this is how I solved the problem:
Make sure you have SSL enabled in your project settings (I do not know why it should be enabled) see image here.
Try this code in Startup.cs:
endpoints.MapAreaControllerRoute(
name: "Counselor",
areaName: "Counselor ",
pattern: "Counselor/{controller=Home}/{action=Index}/{id?}");
Then don't forget to add [Area("Counselor")] at the beginning of the controller like this:
namespace Project.Controllers
{
[Area("Counselor")]
public class HomeController : Controller
{

Customize Authorize Redirect

I am using Beta 4 and when I use the [Authorize] attribute, it redirects to /account/login like i'd expect but that's no the name of my URL. I could customize this in web.config but I don't know where to configure it in ASP.NET 5. Any ideas?
Not sure if this will help but I did download VS 2015 RC.
I’ve created a new MVC 6 website project and launched (F5) it. While being unauthenticated, I tried reaching the ManageController which is decorated with the [Authorize] attribute.
Needless to say, I was redirected to the Account/login view but nowhere have I found where this is configured.
I did manage to add the following inside the ConfigureServices() method of the Startup.cs:
services.Configure<CookieAuthenticationOptions>(options =>
{
options.LoginPath = new PathString("/Gazou/Index");
});
Just above the:
services.AddMvc();
I’ve then created my new GazouController with a simple Index IActionResult().
Ran the application again, tried accessing the ManageController but this time, I was redirected to the Index method of my GazouController instead of the default behavior.
Hope this helps.
Vince
I think you didn't format it with code so I can't see it, but this is what I found:
services.Configure<CookieAuthenticationOptions>(opt =>
{
opt.LoginPath = PathString.FromUriComponent("/Auth/Login");
});

ASP.NET MVC2 Empty Project Not Loading

My environment consists of Visual Studio 2010 and Windows 7 a few months ago I developed an MVC2 application with no problems however after trying to create a new project recently I received the error below.
I did find the link http://support.microsoft.com/kb/894670 but this is not relevant because I am not using IIS for testing, just F5 to get this working :)
Any ideas or help would be appreciated.
Server Error in '/' Application.
--------------------------------------------------------------------------------
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.4952; ASP.NET Version:2.0.50727.4927
The Empty MVC 2 template application does not define any controllers or views. When you create a new Empty MVC 2 application and immediately run it, you will see the error message you posted.
If you check the Global.asax file, you'll see that the project template automatically registers a default route specifying a default controller named "Home" and a default action named "Index".
To get this to run, right-click on the Controllers folder, then choose Add->Controller... Name the controller "HomeController". You can leave the check box to "Add action methods for Create, Update, Delete..." unchecked.
In the HomeController.cs file, right click in the Index() method and choose Add View...
Leave the View name as "Index", uncheck "Select master page", and then click Add.
In the Index view, you can enter some HTML and run the project; you should now see the page rendered by Index.aspx.
One thing I am not sure of is why your error message lists the .NET Framework version as 2.0. If you still have problems, check the target framework on your project properties.
Make sure that the routes are set properly and that you have default values for a controller and action. For example if you have the following route:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
}
);
Make sure that you have a HomeController with an Index action.

Introducing MVC into ASP.Net - 404 error

I have an existing ASP.Net Application, into which I am attempting to introduce MVC2.
Hopefully I can remember the steps, but what I did was the following:
Created a dummy MVC2 project.
Compared and merged .csproj, resulting in the Add Item commands showing MVC2 items.
Compared and merged the web.config
Compared and merged the global.asax.cs
Added Models, Views and Controllers directories
Added HostController with Index action and Index.aspx (no logic)
Amended route to make /Host/Index the default
So now, when I access the application via the root address http://localhost/MyApp it all works.
But, when I access http://localhost/MyApp/Host/Index I get a 404 error. I get the same result for any of the Controller/Actions I created. The only way I can get them to appear is to use the defaults in the routing configuration. I installed Phill Haack's route debugger and it's doing nothing. Obviously there's some problem with my routing, but I can't figure it out.
Any ideas what I've missed?
Bah... turns out that this is to do with IIS 5.1 and MVC routing.
I resolved it by using the following Routes in my application (note the .aspx extensions).
routes.MapRoute("Root", "", new { controller = "Host", action = "Index" });
routes.MapRoute("Default", "{controller}/{action}.aspx", new { controller = "Host", action = "Index" });
Means I can't have clean routes, but at least it works.

Resources