webapi how to route help page to home page - asp.net-web-api

I have an ASP.NET-Web-API project with a generated help area. I would like to route the home page to be pointing to the help page. How can this be done?
I've tried to modify the area registration like below but it is not working
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"HomePage",
"{controller}/{action}/{id}",
new { controller = "Help", action = "Index", apiId = UrlParameter.Optional });
context.MapRoute(
"HelpPage_Default",
"Help/{action}/{apiId}",
new { controller = "Help", action = "Index", apiId = UrlParameter.Optional });
HelpPageConfig.Register(GlobalConfiguration.Configuration);
}

Related

Rendering of #Ajax.ActionLink is messed up by adding a new route

I have an ActionLink:
<td>
#Ajax.ActionLink(item.HostedBy, "_DetailsMerged", "Marcher",
new { id = item.HostedById },
new AjaxOptions
{
UpdateTargetId = "marcherId" + #i ,
HttpMethod="GET",
InsertionMode = InsertionMode.Replace,
})
</td>
that displays the appropriate link when I just have default routing. However, introducing a route for a geolocation proxy, (which implments IRouteHandler and is a custom route handler) BEFORE the default routing:
routes.Add("WebgisUscproxy", new Route("WebgisUscproxy", new OpenLayers_Bronze.Utility.WebgisUscProxyHandler()));
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
while working OK for semi-hard-coded links in my client javascript ajax call (that look like: /WebgisUscproxy?zip=10003), screws up how the #Ajax.ActionLink links are rendered. They ALSO pick up '/WebgisUscproxy'. So I get:
http://localhost:59723/WebgisUscproxy?action=_DetailsMerged&controller=Marcher&id=1
instead of:
http://localhost:59723/Marcher/_DetailsMerged/1
which is what I want, and get with just the default routing. Putting:
routes.Add("WebgisUscproxy", new Route("WebgisUscproxy", new OpenLayers_Bronze.Utility.WebgisUscProxyHandler()));
AFTER the default route handler also doesn't work, as the default routing now matches semi-hard-coded links, like '/WebgisUscproxy?zip=10003'.
==========================================================================
I've found a work-around, though I hope somebody posts something more direct.
routes.MapRoute("webgisusc", "WebgisUscProxy",
new { controller = "Utility", action = "WebgisUsc" }
).RouteHandler = new OpenLayers_Bronze.Utility.WebgisUscProxyHandler();
I had created an empty Utility controller, and was going to add a dummy "WebgisUscProxy"
action to it, but it turns out that there's no need for even a dummy UtilityController. This route definition works, all by itself.

ASP.NET MVC 3 Controller route - make everything under home controller appear under the domain

Currently everything under homecontroller appears in the URL as
example.com/Home/{Action}
Is there a way we can keep all other routing the way it is but ONLY special case home controller so everything under home comes under the domain.
like
example.com/about
example.com/contact
example.com/error
instead of creating new controller classes for each of them.
EDIT:
The other URL's like
example.com/user/details/123
example.com/user/edit/123
Which are in the userController should work the same as they are now
I think the best way is:
routes.MapRoute("home", "home", new { controller = "Home", action = "Index" });
routes.MapRoute("about", "about", new { controller = "Home", action = "About" });
routes.MapRoute("contact", "contact", new { controller = "Home", action = "Contact" });
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
and when you want to create a link, use:
#Html.RouteLink("Home", "home", new{/* route values */}, new {/* html attribues */})
OR:
#Html.RouteLink("Home", "home")
instead of:
#Html.ActionLink("Home", "Index", "Home", new{/* route values */}, new {/* html attribues */})
this works for me, and should work for you too.
UPDATE:
you can create a symbol like # (or - or anything else), before the action part in url, to make the url unique, such as:
routes.MapRoute(
"test", // route name
"#{action}", // url and parameters
new {controller = "MyHome", action = "Home"} // parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
in this way, your urls are different from the Default map-route and you can create urls like:
site.com/#Home
site.com/#About
site.com/#Contact
but the first, in my idea, is better and I always use that.
Using the Attribute Routing of MVC5, I did similar to Javad_Amiry answer, by placing a route for each action in HomeController:
public class HomeController : Controller
{
[Route("about")]
public ActionResult About()
{
return View();
}
I think this is more maintainable than placing every action in the global RouteConfig.cs file. Better still to combine Attribute Routing with convention-based routing, so new actions added to controller will work by default without a Route attribute (eg: /Home/action) but can be improved by adding the Route attribute (eg: /action).
You could simply modify the default route and remove the controller bit from the url and specify that it will always be Home in the default values:
routes.MapRoute(
"Default",
"{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Obviously you realize that this limits your application to a single controller which is HomeController as now you no longer have any possibility to set it in your url. Stuffing all the actions in a single controller is a bad practice IMHO and violates a couple of principles like RESTful routing and SRP.
ASP.NET MVC root url’s with generic routing

MVC 3 routing problem

In my site i have the following routing table:
routes.MapRoute("Something",
"sites/{company}/{site}/",
new { controller = "Home", action = "LoadClientSite" },
new[] { "CorrectOnLine.Controllers" });
routes.MapRoute("Default1", // Route name
"{company}/{site}/", // URL with parameters
new { controller = "Home", action = "DefaultRedirect" },
new[] { "CorrectOnLine.Controllers" }
);
routes.MapRoute("Default2", // Route name
"{company}/{site}/{id}", // URL with parameters
new { controller = "Home", action = "DefaultRedirect" },
new[] { "CorrectOnLine.Controllers" }
);
I am using the following link to call an action:
#(Html.Action("index", "home", new { area = "ClientsSites", CompanyID = Model.CompanyID, SiteID = Model.SiteID, CategoryID = Model.CategoryID, Message = Model.Message }))
In the view i have an image:
<img alt="" src="../../../../../WareHouse/Sites/logo_site_#(Model.Site.SiteID).jpg" height="250px" width="750px" />
The problem is that when the image loades MVC tries to locate the image src in the routing table , conntroller = WareHouse, action = Sites.
How can i make MVC to only load the iamge and not try to load it as a view?
Thanks,
Alex
You could ignore route for all paths that end in the .jpg extension...
routes.IgnoreRoute("{*alljpeg}", new {alljpeg=#".*\.jpg(/.*)?"});

MVC with Areas -- Html.ActionLink returning wrong URL/Route?

I am using MVC3 and have Areas in my application. In general, everything works fine, I can navigate to my area (e.g. Admin=Area, Controller=Department) like this:
<%: Html.ActionLink("Departments", "DepartmentIndex", "Department", new { area = "Admin"}, null )%>
However, what I noticed is that if I don't specify the area in my ActionLink, e.g.
<%: Html.ActionLink("Test", "Index", "Home")%>
This will stop working if I have navigated to the "Admin" area. i.e. my url is now
http://localhost/myproj/Admin/Home/Index
instead of
http://localhost/myproj/Home/Index
Any ideas on what is going on here?
My routes are all the defaults when creating an MVC app / Areas. i.e.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "MyProj.Controllers" }
);
}
And my area registration
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
Is this by design?
This posting suggests using RouteLink instead of ActionLink:
say to use RouteLink
Is it required to have "area" routevalue on actionlink if the application was grouped into areas?
This StackOverflow answer correctly states that you need to provide the Area name if you're using areas.
eg.
Html.ActionLink("Home", "Index", "Home", new { Area = "" }, new { })

LogOnPartial renders with a blank href - MVC 3

I'm using a standard MVC 3 Web Project and I've written my HTML out in a standard HTML file, and then copy/pasted it into the _layout.cshtml file. With all the correct RenderBody() and #Html.Partial("_LogOnPartial") included the page works fine, but the ActionLink inside the _LogOnPartial doesn't render an href.
Html.ActionLink("Log On", "LogOn", "Account")
Will come out as:
Log On
This is unchanged from the standard link that you get when you start an MVC 3 Web project.
The registered Routes are:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Products",
"{controller}/{action}/{id}",
new {controller = "Products", action = "Index"});
routes.MapRoute(
"Newsletter",
"{controller}/{action}/{emailAddress}",
new { controller = "Newsletter", action = "Index" });
routes.MapRoute(
"Account",
"{controller}/{action}/{id}",
new { controller = "Account", action = "LogOn", id = UrlParameter.Optional });
}
I don't understand why this is happening, but in Visual Studio it is not underlining the Action or Controller which makes me think it isn't seeing the AccountController properly. Anyone got any ideas?
Fabian is right. The problem is because there are too many routes, many of which are almost identical in their pattern. Your "Default", "Products" and "Account" routes all look for a controller, an action and an optional ID (either explicitly or implicitly). You would probably have considerably more success if you just set the default controller back to its factory-standard form like so:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
}); // Parameter defaults
routes.MapRoute(
"Newsletter",
"{controller}/{action}/{emailAddress}",
new { controller = "Newsletter", action = "Index" });
}
Since the ID is optional, your Products routes will still be matched if you supply an ActionLink with no ID (e.g. #Html.ActionLink("My Link", "Products", "Index")) and the form of the "Account" route is identical to the "Default" one anyway. There's no need to use it and MVC is likely being tripped up between these, which is causing your LogOn partial's Href to fail.
ActionLink doesn't look at the controller or it's actions, just the routing table. Make sure you have a route with action "LogOn" and controller "Account" set in your global.asax.
Edit:
I recommend you read up a bit on mvc routing. Your current routes are too greedy and will match when they shouldn't.
For LogOn i would use something like this (put it at the top because its the least greedy since it doesnt contain any variables):
routes.MapRoute(
"Account",
"logon",
new { controller = "Account", action = "LogOn", id = UrlParameter.Optional });

Resources