I have a route defined
routes.MapRoute(
name: "NoRightsRoute",
url: "norights",
defaults: new { controller = "Home", action = "NoRights" }
);
and in a check on if the user has rights I redirect them to this route if they don't
Response.RedirectToRoute("NoRightsRoute");
the url that it redirects to is
https://localhost/norights
Home is removed which I would expect since that is assumed by MVC but I get an error
"The resource could not be found"
if I change the url to
https://localhost/Home/norights
the page loads correctly. Why is "Home" being required in my url? Is there some way to have "Home" included in the url so that the redirect works or get the page to load as it should when assumed?
You may try to remove the default, and hard code Home in url.
routes.MapRoute(
name: "NoRightsRoute",
url: "Home/norights",
);
But check that other links /redirections are not impacted !
Related
I am trying Visual Studio 2019 and ASP.Net Core.
I have a small sample site running on my desktop (development). I can launch the site in DEBUG mode and a browser comes up. The initial URL displayed is just:
https://localhost:12345/
I would like it to be:
https://localhost:12345/Home/Index
or whatever the starting controller and view will be. I can append the controller and view to the initial URL but is there a way to have them there by default? I have done some goggling but I must not know the right terms to use.
Ideas?
Edit
my routes in startup.cs look like:
app.UseMvc(routes => {
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
It does not appear that .Net Core 2.1 offers a url option.
Edit 2
My goal is to literally have the "default" controller and view names appear in the browser's URL when I get to the "home" page. As of this moment, when I launch the app in a browser in debug mode, all I see in the URL display is "https://localhost:12345/". I have not tried to publish this so I do not know what a production version might do.
According your request ,you could use a Rewriter middleware as follow:
var rules = new RewriteOptions()
.AddRedirect(#"^.{0}$", "/Home/Index");
app.UseRewriter(rules);
app.UseMvc(
routes =>
{
routes.MapRoute(
name: "defaults",
template: "{controller=Home}/{action=Index}/{id?}");
}
);
I'm not sure why you'd want this, as the current behavior is actually better and more standard. However, the way to stop it is to remove the defaults from the route:
app.UseMvc(routes => {
routes.MapRoute(
name: "default",
template: "{controller}/{action}/{id?}");
});
Then, the controller/action portions of the route will be required, and HomeController.Index can only be access by literally specifying the route /Home/Index. However, that's going to remove your root route, so going directly to https://localhost:12345, without any path, will throw a 404.
I am trying to create a new route... Here the code:
routes.MapRoute(
name: "Services",
url: "Administration/{controller}/{action}/{id}",
defaults: new { controller = "Services", action = "Index", id = UrlParameter.Optional });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
Now when I click on an actionlik, it redirects me on the correct controller:
#Html.ActionLink("Services", "Index","Services")
Here the Index Action in the Services controller
public ActionResult Index()
{
return View(); //Here it is where I stop debug
}
I arrive in the action.. and now my custom route should redirect to my view. Correct?
I let you see what I see when i stop debug:
How you can see everything seems is well valorized. But when I obtain following error:
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Services/Index.aspx
~/Views/Services/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Services/Index.cshtml
~/Views/Services/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
It does not look the view inside Administration folder!!
In this way it works instead:
return View("~/Views/Administration/Services/Index.cshtml");
Where is wrong?
Thank you
Looks like you don't quite understand routing, or that routing is not the same thing as view resolution. Changing the URL to access an action method doesn't make it search new folders for your views. So you need to do one of the following:
Leave the view in one of the folders the error message suggests.
Manually specify the view: return View("~/Views/Administration/Services/Index.cshtml");
Learn how to use MVC areas.
Manually add in your additional folders to the list the view engine uses.
Implement your own view engine.
Personally I recommend option 1.
I'm new to the whole routing thing in MVC so I'm not sure what to make the title of my question. Basically, we have Google Analytics that pics up stats based on the current url. We added in a referrer to the route so that we can give a link to our partner sites such as:
http://www.mysite.co.za/PartnerSite/home/index
When the user comes to our site directly, ie: http://www.mysite.co.za, I want the url to show "Website" as the referrer.
So in essence,
http://www.mysite.co.za
must change to
http://www.mysite.co.za/Website/Home/Index
My route is current configured as:
routes.MapRoute(
name: "Search",
url: "{referer}/{controller}/{action}",
defaults: new { referer = "Website", controller = "Home", action = "Index" }
);
Can this be done in the routes or should I get my system administrator to update IIS settings and redirect?
Thanks,
So to close this off I needed to have a solution posted.
The bottom line is that this is something for IIS, and it does not seem possible using Routes.
ok,suppose when the user hits the url http://www.mysite.co.za then suppose it accessing Home Controller's Index View Action.Because /Home/Index is the First Default Page in MVC so its not the Part of URL.
So for your Case You should write the code like this-
routes.MapRoute(
name: "Website",
url: "/Website/Home/Index",
defaults: new { controller = "Home", action = "Index" }
);
Please Try it and let me know the result.
I am planning to write custom ASP.NET MVC4 routing for the problem described below.
By default [http://localhost:1603] URL should show login page after user logged in then URL should contain username name in the URL like [http://localhost:1603/username] and other action methods should access like [http://localhost:1603/username/profile].
Please guide me how to proceed with this problem.
Thanks in advance.
By default [http://localhost:1603] URL should show login page
To do this you can change Default Url Route's default values.Like this:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
You should change default Controller and Default Action name.For example I change them like: Account and Login.
URL should contain username name in the URL like [http://localhost:1603/username]
Now you need a custom route,maybe something like:
routes.MapRoute(
name: "Custom",
url: "{username}/{action}",
defaults: new { controller = "UserController", action = "Index" }
);
With this route (I assume you have (UserController and an Index action in it):
http://localhost:1603/username
Will go to the Index action of UserController.
http://localhost:1603/username/profile
Will look for a Profile Action inside of UserController.Ofcourse you should get username from RouteData dictionary inside of your Action.Otherwise that seems pointless.
I'm a fan of MvcCodeRouting (there's a NuGet package for it). Take a look at the doco for base routes. It will do what you're trying to achieve.
I have created an ASP.NET WebApplication with Internet Template.
I have NOT modified anything in the RouteConfig class, meaning I am using default Route table only.
When I type the following URL insde the browser
http://localhost:8416/
Index view is retuning insde HomeController without any problem.
But, if I type the following URL
http://localhost:8416/EstimationTracker/
I am gettinger the following error.
HTTP Error 403.14 - Forbidden
The Web server is configured to not list the contents of this directory.
But, if I type the following URL, view is returning fine (just appending /Index to the above URL).
http://localhost:8416/EstimationTracker/Index
My doubt is why Index action method is being not recognized as the default action method inside EstimationTracker controller or is something else happening?
For easy referece, the following is the code inside inside RouteConfig.cs file.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
EstimationTracker folder created in the project root folder is causing the problem.
The error in the question means that the static file handler took the request.
I got the answer from here.
Is there a folder called EstimationTracker in you application's root folder?