Area routing in ASP.NET Core 5.0 - visual-studio

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
{

Related

Codeigniter 4 - controllers won't work unless I add them directly to Routes.php

Can anyone tell me:
Do I have to declare all of my controllers in Routes.php in Codeigniter 4?
I can't seem to get a controller to work unless I add it directly to the "Routes.php"
I have created my controllers properly and the Home controller is working after install and setup.
If I add the controller My_page.php :
<?php
namespace App\Controllers;
class My_page extends BaseController{
public function index(){
echo "Controller 'My_page' -> function index() ";
}
}
?>
I get a
: "404 - File Not Found
Sorry! Cannot seem to find the page you were looking for."
If I now add the controller to the rout - i.e.:
$routes->post('my_page', 'My_page::index');
Then my controller works properly and I get the "Controller 'My_page' -> function index() " when I visit www.mydomain.com/my_page
I have also tested:
www.mydomain.com/index.php/my_page
and this makes no difference.
I am using the .htaccess that comes with the download. I have updated the base URL to www.mydomain.com/
The documentation is confusing to me - https://www.codeigniter.com/user_guide/incoming/routing.html#setting-routing-rules ;
it sounds like they are saying have to declare all classes with routes?
Why are my controllers not working without declaring them specifically in Routes.php?
And am I misunderstanding 'setAutoRoute(true)' it also does not seem to work - I expect that I can turn this on and simply create my controllers pretty much like in CI3?
If you don't enable auto-routing you most certainly need to add all routes which you are allowing, anything else will fail with error 404. As #parttimeturtle mentioned - autoroute it is disabled by default since 4.2.
So in short - Yes, you need to add all controllers, their functions and the appropriate http methods. (That includes CLI routes as well)
You can use $route->add(), which will allow all http methods, it's however more secure to explicitly set them with their methods.

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

Sitecore Wrong Route in MVC

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(...)

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");
});

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