ignore directory while redirecting ASP.NET MVC 4 - asp.net-mvc-4

i have URL http://172.16.59.152/ITMS_PNS_TEST/DashboardDailyDuty/DashboardDailyDuty
in ASP.NET MVC 4,,
i want to ignore ITMS_PNS_TEST when redirecting to another page how to do????
my RouteConfig is
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}/{id1}",
defaults: new { controller = "Login", action = "ScheduleLogin", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "MultipleDutyOption",
url: "{controller}/{action}/{id}",
defaults: new { controller = "MultipleDutyOption", action = "RouteMergeApply", id = UrlParameter.Optional }
);
}

Related

Issue with default path in MVC

I am not able to set the default page in MVC. I am using lowercase-dashed-routing of git hub for url. When I open my project, I am getting 404 error. When I open page like localhost/home/index, it is working fine.
using LowercaseDashedRouting;
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("");
//routes.MapRoute(
// name: "Default",
// url: "{controller}/{action}/{id}",
// defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
// );
routes.Add(new LowercaseDashedRoute("{controller}/{action}/{id}",
new RouteValueDictionary(
new { controller = "home", action = "Index", id = UrlParameter.Optional }),
new DashedRouteHandler() ) ); }}}

MVC RouteConfig, Route determinated controllers

I want access to my principal pages (index, about, contact...) only with {action}{id}, but to the others with {controller}{action}{id}. For example:
A principal page: myweb.com/index
No principal page: myweb.com/Account/Login
I've searched but I don't know how do it exactly.
This is my RouteConfig
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
/*routes.MapRoute(
"Account",
"Account/{AccountId}",
new { action = "Index", controller = "Course" }
);*/
/*routes.MapRoute(
name: "Default",
url: "{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);*/
/*routes.MapRoute(
"Account",
"Account/{courseId}",
//new { action = "Index", controller = "Course" }
new { controller = "Home", action = "Index" }
);*/
//routes.Add()
}
}
Change your routes config as below but you must have all your principal actions in one controller say PrincipalController as you will not be having controller in your routes.
Always map the high priority routes before low precedance routes.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Principal",
url: "{action}/{id}",
defaults: new { controller = Princpal", action = "Index", id = UrlParameter.Optional }
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Please see example:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Principal pages have unique route per each page.
// Methods About and Contact should be implemented in HomeController.
routes.MapRoute(
name: "About",
url: "about",
defaults: new { controller = "Home", action = "About" }
);
routes.MapRoute(
name: "Contact",
url: "contact",
defaults: new { controller = "Home", action = "Contact" }
);
// other pages use this default route
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}

Make custom default page in asp.net mvc

I would like to add custom default page in Asp.net mvc so instead the page going to Home/Index , i would like to go to Account/Login. I have implement the following but it still go to Home/Index. Please advise what i did wrong. Thank you
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Custom",
url: "{controller}/{Account}/{page}",
defaults: new
{
category = UrlParameter.Optional,
page = 1,
action = "Login"
},
constraints: new
{
controller = "Account"
}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Assuming the case that you want the users to be redirected to the login page or a custom page if they are not 'signed-in'.
You could create a filter attribute.
example:
[RequireHttps]
[AuthorizationFilter]
public class MyController : Controller
{
}
public class AuthorizationFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//do some validation, find if the user is signed-in.
filterContext.Result = new RedirectResult(..Some where in the site..);
}
}
Change your route. The default route is set to /Home/Index
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters*
new { controller = "Home", action = "Index",
id = UrlParameter.Optional }
);
You can change that to be any route you wish
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters*
new { controller = "ControllerName", action = "ControllerActionName",
id = UrlParameter.Optional }
);
replace your default route (the last one) with this
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);

setting custom path in routeconfig.cs in mvc 4

Below is my routeconfig.cs file in mvc4 application
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{bizId}",
defaults: new { controller = "Home", action = "Index", bizId = UrlParameter.Optional }
);
routes.MapRoute(
"Outlet",
"Outlet/{bizId}",
new { controller = "Home", action = "Index" },
new { bizId = UrlParameter.Optional }
);
}
When i run the application, i need to enter /Home/Index?bizId=1 or any Id after the localhost port to run my application. It is working fine. But, now as the second route.maproute, I want the url to show as for ex: localhost:49787/Outlet?bizId=1 but this doesnt work. Please help! Thanks in advance
got the fix:
routes.MapRoute(
name: "Outlet",
url: "Outlet/{bizId}",
defaults: new { controller = "Home", action = "Index", bizId = 1 }
);

ASP.NET MVC 4 Routes not working

Route
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Contact",
url: "Contact",
defaults: new { controller = "Home", action = "Contact" }
);
my controller
public class HomeController : BaseController
{
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
My Global.asax
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
}
Finally my request url
http://localhost:1234/Contact/
Error on browser
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: /Contact/
Version Information: Microsoft .NET Framework Version:4.0.30319;
ASP.NET Version:4.0.30319.18033
What am I doing wrong?
Solution:
Custom route should take the precedence
routes.MapRoute(
name: "Contact",
url: "Contact",
defaults: new { controller = "Home", action = "Contact" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The Framework always tries to match the URL of the request to a route in the order of the Routes added to the RouteCollection
So you should put the custom routes before the default route,
//Custom route
routes.MapRoute(
name: "Contact",
url: "Contact",
defaults: new { controller = "Home", action = "Contact" }
);
//default route
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
You can use:
routes.MapRoute(
name: "Default",
url: "{*p}",
defaults: new { controller = "Home", action = "Index", p = UrlParameter.Optional }
);
The asterisk indicates that it's a catch-all route. Keep in mind that these routes are extremely greedy, make sure that this stays below any specific routes.
You could also add a route constraint to this route which can determine whether the page exists in the database or something.