RouteJs allows you to use your ASP.NET MVC or WebForms routes from JavaScript. It does not depend on jQuery or any other JavaScript framework, so can be used in any scenario. RouteJs works with your existing MVC routes, you do not need to use a different routing syntax or modify any of your existing routes at all.
Bug reports and feature requests are welcome!
Required:
Install-Package RouteJs.Mvc4
(replace RouteJs.Mvc4
with RouteJs.Mvc3
for ASP.NET MVC 3 or RouteJs.Mvc2
for ASP.NET MVC 2)
Alternatively, you can get the latest development build from the build server.
Once installed, you need to reference the RouteJs handler in your view. This serves the JavaScript and route information:
<script src="@RouteJs.RouteJsHandler.HandlerUrl"></script>
Install-Package RouteJs.AspNet
Once installed, you need to add RouteJs to your Startup.cs
file, and also ensure IActionContextAccessor
is registered:
using Microsoft.AspNetCore.Mvc.Infrastructure;
using RouteJs;
...
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
services.AddRouteJs();
The RouteJs handler also needs to be referenced in your view (generally _Layout.cshtml
is a
good place for this). This serves the JavaScript and route information:
@inject RouteJs.IRouteJsHelper RouteJs
@RouteJs.Render()
The main function is Router.action
. This accepts three parameters:
Examples:
// Using the default route
var url = Router.action('Controller', 'Action'); // eg. /Controller/Action
// Handling optional parameters
var url = Router.action('Foo', 'Bar', { id: 123 }); // eg. /Foo/Bar/123
// Appending querystring parameters
var url = Router.action('Foo', 'Bar', { hello: 'world' }); // eg. /Foo/Bar?hello=world
The routes that are exposed are controlled by the web.config "exposeAllRoutes" setting for ASP.NET MVC 5 and below:
<routeJs exposeAllRoutes="true" />
And via Startup.cs
for ASP.NET MVC 6:
services.AddRouteJs(config => {
config.ExposeAllRoutes = true;
});
If set to "true", all of your ASP.NET MVC routes will be exposed to JavaScript, unless you
explicitly hide them via the HideRoutesInJavaScript
attribute on a controller. If set to "false",
all routes will be hidden unless you explicitly use the ExposeRoutesInJavaScript
attribute on the
controller. These two attributes currently affect all routes for the controller.
AddRouteJs
method to make configure
argument optional/
as default base URL when empty/
Router.action
call(The MIT licence)
Copyright (C) 2013 Daniel Lo Nigro (Daniel15)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.