How to route from a area to the default route - asp.net

I have a front page and a CMS area with the following routes:
Default Front Page route
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "SiteFactory.Site.Controllers" }
Administration route
context.MapRoute(
"Administration_default",
"administration/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I would like to route from my ContentController (inside the administration area) to the front page HomeController, like this:
[HttpPost]
[ValidateInput(false)]
public ActionResult Save(string content, string contentId, string pageId)
{
if (ModelState.IsValid)
{
//TODO: save content.
}
return RedirectToRoute("Default");
}
How can i do this?

just return RidirectToAction("Index", "Home");

Related

Serving static SPA resources in ASP.NET MVC 4

I'm writing an ASP.NET MVC 4 app (I can't use any framework versions newer than 4.0 for this project) with React SPA client.
I have a problem serving static resources like js files and stylesheets. In he heat of a fight I came with something like this:
routes.MapRoute(
name: "Static1",
url: "{filename}.{extension}",
defaults: new { controller = "Home", action = "Static1" }
);
routes.MapRoute(
name: "Static2",
url: "{url}/{filename}.{extension}",
defaults: new { controller = "Home", action = "Static2" }
);
routes.MapRoute(
name: "Static3",
url: "{url1}/{url2}/{filename}.{extension}",
defaults: new { controller = "Home", action = "Static3" }
);
routes.MapRoute(
name: "Empty",
url: "",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{*path}",
defaults: new { controller = "Home", action = "Index" }
);
and
public ActionResult Index()
{
return new FilePathResult("~/client/index.html", "text/html");
}
public ActionResult Static1(string filename, string extension)
{
return Redirect(Url.Content($"~/client/{filename}.{extension}"));
}
public ActionResult Static2(string url, string filename, string extension)
{
return Redirect(Url.Content($"~/client/{url}/{filename}.{extension}"));
}
public ActionResult Static3(string url1, string url2, string filename, string extension)
{
return Redirect(Url.Content($"~/client/{url1}/{url2}/{filename}.{extension}"));
}
The problem is that webpack often generates files with multiple dots in their name (ex. main.4395d64a.js) and thus even this already not pretty solution fails. I would rather not create multiple routes for different dot numbers.
Is there a better way?

MVC 5.0 RegisterRoutes not working

Anyone know why this is not working, i have googled it but nothing seems to work. All i want to do is allow www.abcdefg.com/username to map to a profile page but it does not work and i can't find out why.
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
, new { controller = #"(admin|help|profile|Settings)" } // Constraints
);
routes.MapRoute(
"Users",
"{username}",
new { controller = "Home", action = "Index", username = "" });
}
You must adding the default route as the last route
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
"Users",
"{username}",
new { controller = "Home", action = "Index", username = "" });
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
, new { controller = #"(admin|help|profile|Settings)" } // Constraints
);

Url Route asp.net

my Model is
public class Organization
{
public int ID { get; set; }
public string Url { get; set; }
}
and this is my routeConfig
routes.MapRoute(
"Url",
"{controller}/{action}/{Url}",
new { controller = "Organization", action = "PageContent", Url = "" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Is there a way to fix the routing so that both Url and ID would work?
for example ...Organization/details/{ID} and ...Organization/details/{Url} would both work
As both the routes seems same you can combine both and define one and in the action (or onActionExecuting) you can decide the behavior based ID or URL
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{Segment}",
defaults: new { controller = "Home", action = "Index", Segment = UrlParameter.Optional }
);
Or you can map the route with ID only and take URL as querystring.

Show custom url?

How to display custom url and not based on controller and action names?
My routing looks like
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(
name: "productfeature",
url: "cn/pendrive/18-gb",
defaults: new { controller = "Product", action = "Feature", id = UrlParameter.Optional }
);
}
}
and my link button looks like
<li>#Html.RouteLink("Product Feature", "productfeature")</li>
I get url as the Url mentioned in the code but i also 404 error.
http://localhost:9090/cn/pendrive/18-gb
How to go for static urls? Basically my aim is to no show controllers and action names in url.
Put your custom route before the default one:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "productfeature",
url: "cn/pendrive/18-gb",
defaults: new { controller = "Product", action = "Feature", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}

MVC4 Default Route to point to an area

I have 2 areas in my project:
Areas | Admin
Areas | FrontEnd
What I would like is when I visit the site, the default route should load Controllers / Views / Models from the FrontEnd area. It's normal to have Url/Admin for an admin panel but I would rather not have to force Url/FrontEnd (or some other variation). Basically I don't want to use the Controller / Model / View folders on the root level.
I'm not sure how to change the code to allow this or even it's an advisable method. Could someone provide some guidance please?
What I have:
routes.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new {
area = "Admin",
controller = "Home",
action = "Index",
id = UrlParameter.Optional
},
namespaces: new[] { "WebsiteEngine.Areas.Admin.Controllers" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {
area = "FrontEnd",
controller = "Home",
action = "Index",
id = UrlParameter.Optional
},
namespaces: new[] { "WebsiteEngine.Areas.FrontEnd.Controllers" }
);
However this produces an error:
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Home/Index.cshtml
~/Views/Home/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
I do have views available in the areas and this doesn't look like it's looking there.
I believe you can just do something like this:
// Areas/Admin/AdminAreaRegistration.cs
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get { return "Admin"; }
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
name: "Admin_Default",
url: "Admin/{controller}/{action}/{id}",
defaults: new
{
area = "Admin",
controller = "Home",
action = "Index",
id = UrlParameter.Optional
});
}
}
// Areas/Admin/FrontEndAreaRegistration.cs
public class FrontEndAreaRegistration : AreaRegistration
{
public override string AreaName
{
get { return "FrontEnd"; }
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
name: "FrontEnd_Default",
url: "{controller}/{action}/{id}",
defaults: new
{
area = "FrontEnd",
controller = "Home",
action = "Index",
id = UrlParameter.Optional
});
}
}
// Global.asax.cs
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
...
}
Now, in your RouteConfig class, you probably have a Default route set up. Bear in mind that as long as you call AreaRegistration.RegisterAllAreas before you call RouteConfig.RegisterRoutes, the routes that you set up in the areas may override the routes you set up in RouteConfig. (Routes are evaluated in the order they appear in the Routes collection, and .MapRoute pushes new routes to the end)

Resources