Razor Pages Custom Page Routing - c#

This question has been asked earlier and as I am new to Razor Pages, unable to figure out how I can make the required (Silly one). So here it's: I've a razor page where I show a list of data where I've to make the page routing or url as follows:
#foreach (var item in Model.Data)
{
#item.Name #item.City #item.State
}
So pretty simple, so in the Startup.cs, I tried to do the following to make it work. Unfortunately failed:
//This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc();
services.AddEntityFrameworkSqlite().AddDbContext<MyDbContext>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddMvc().AddRazorPagesOptions(options =>
{
//Here is the configured routing that I tried
options.Conventions.AddPageRoute("/search_details", "/search_details/id");
});
}
I would like to return the url as ./search_details/id/1002 and in the back-end, did this to get the query string value:
string id = this.RouteData.Values["id"].ToString();
This is pretty basic, but as I said failed (Failed means in the back-end, the query string doesn't hit while debugging). I've one more scenario and this thing is bothering me right now the most. My requirement is to hide the page name as well. Something like this:
localhost:8080/search_details/id/1002
To
localhost:8080/1002
Is there appropriate way where I can accomplish that? I understand that's a bad practice for other purposes but for the time being, I would go with it.
N.B: I am not willing to do it in the client-end unless there is no alternate. If possible, better with something server-side using C#.

You can use Route Templates in Razor pages :
https://www.learnrazorpages.com/razor-pages/routing
Comment out this line options.Conventions.AddPageRoute , and in your search_details page add nullable parameter :
#page "{id?}"
In cs file , you can get route data like :
public void OnGet(string id)
{
}
For the second requirement, you can add template like :
options.Conventions.AddPageRoute("/search_details", "{id}");
So that localhost:8080/1002 will redirect to search_details page and you will get id as route data also .

Related

Page route prevents OnGet() receiving query argument

I'm porting a WebForms application to Razor Pages, and I'm using the following route to prevent breaking one of my URLs.
services.AddMvc().AddRazorPagesOptions(options =>
{
options.Conventions.AddPageRoute("/Resources/CharClass", "/Resources/CharClass.aspx");
});
But using the code below, when I use the URL /Resources/CharClass.aspx?page=500, page is always null.
cshtml
#page "{page:int?}"
cshtml.cs
public void OnGet(int? page)
{
//
}
The argument seems to work okay for pages that aren't routed this way. How can I have both?
I suspect that the problem you have is with the name of the parameter - page. This seems to conflict with the Page property of the PageModel class, and nothing is bound during model binding. You can however access the query string value using traditional methods:
var page = Request.Query["page"];
EDIT: Just seen that this is partly covered in a reply to your other question on the issue: Razor page link ignores route argument
Try:
services.AddMvc().AddRazorPagesOptions(options =>
{
options.Conventions.AddPageRoute("/Resources/CharClass", "/Resources/CharClass.aspx/{page?}");
});

Swagger not loading - Failed to load API definition: Fetch error undefined

Trying to setup swagger in conjunction with a web application hosted on IIS express. API is built using ASP Net Core. I have followed the instructions prescribed on the relevant microsoft help page regarding Swashbuckle and ASP.NET Core.
Thus far I have got the swagger page to load up and can see that the SwaggerDoc that I have defined is loading, however no API's are present. Currently am getting the following error:
"Fetch error undefined ./swagger/v1/swagger.json"
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// services.AddDbContext<TodoContext>(opt =>
// opt.UseInMemoryDatabase("TodoList"));
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
// Register the Swagger generator, defining 1 or more Swagger documents
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "API WSVAP (WebSmartView)", Version = "v1" });
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
// specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("./swagger/v1/swagger.json", "My API V1");
c.RoutePrefix = string.Empty;
});
app.UseMvc();
}
}
So after a lot of troubleshooting it came down to basically two things, but I feel that in general this could be helpful to someone else in the future so I'm posting an answer.
First- if ever your stuck with the aforementioned error the best way to actually see whats going on is by adding the following line to your Configure() method
app.UseDeveloperExceptionPage();
Now if you navigate to the 'swagger/v1/swagger.json' page you should see some more information which will point you in useful direction.
Second- now for me the error was something along the lines of
'Multiple operations with path 'some_path' and method 'GET' '
However these API were located inside of dependency libraries so I was unable to apply a solution at the point of definition. As a workaround I found that adding the following line to your ConfigureServices() method resolved the issue
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "API WSVAP (WebSmartView)", Version = "v1" });
c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First()); //This line
});
Finally- After all that I was able to generate a JSON file but still I wasn't able to pull up the UI. In order to get this working I had to alter the end point in Configure()
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("./v1/swagger.json", "My API V1"); //originally "./swagger/v1/swagger.json"
});
I'm not sure why this was necessary, although it may be worth noting the web application's virtual directory is hosted on IIS which might be having an effect.
NOTE: Navigating to swagger/v1/swagger.json will give you more details, for me it was causing issue due to undecorated action. This information is mentioned in comment by #MarkD
I've been working with .Net Core 3.1 and I spent some time to find out and understanding what was going on.
The issue can arise from many different reasons:
Swagger configuration errors
Classes with the same name but in different namespaces
Public methods without the rest attribute (Get, Post, etc.)
First, take a look the link below just to check if your setup is ok:
Add Swagger(OpenAPI) API Documentation in ASP.NET Core 3.1
Then,
A good tip to find out the problem is to run the application without to use IISExpress and check the console log. Any error found to generate the documentation will be displayed there.
In my case, the problems was that I had a public method (that should be private) without any rest attribute:
After change the method from public to private I solve the issue.
I was able to find the error by opening the network tab and looking at the response for swagger.json
Simply navigate to https://localhost:{PortNo}/swagger/v1/swagger.json and get much more details about the error message.
also I had similar problem in .NET 5.0, I solved below way:
I added this line as attribute over controller:
[Consumes("application/json")]
I've been working with .NET 5 and I spent some time trying to understand what was going on.
I got an error like the one below:
Then I resolved this problem by the following:
Open startup.cs file
Add the following code in Configure method
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger(c =>
{
c.RouteTemplate = "/swagger/{documentName}/swagger.json";
});
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1"));
}
And in ConfigureServices method
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "API", Version = "v1" });
c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
});
Thanks to TheCodeBuzz for Resolved: Failed to load API definition (undefined /swagger/v1/swagger.json)
Note the difference between the RouteTemplate string and the SwaggerEndpoint string. One uses {documentName} and the other uses "v1" as a literal.
I've come across the same error before, after struggling to find the reason, I discovered that one of my API in one of my controllers have no HTTP verb as an attribute, So I fixed it by putting [HttpGet] on my API.
So here is my advice, check your API controllers, maybe you forget the same thing as me!
Take a look at my code, I realized that I should change this :
public async Task<Product> ProductDetail(int id)
{
return await _productQueries.GetProductDetail(id);
}
to this:
[Route("ProductDetail")]
[HttpPost]
public async Task<Product> ProductDetail(int id)
{
return await _productQueries.GetProductDetail(id);
}
I had similar issue, I solved it using the Route attribute on the offending controller method:
[HttpGet, Route("Throw")]
public ActionResult<string> Throw()
{
_logger.LogInformation("I think I am going to throw up");
throw new NotSupportedException("For testing unhandled exception logging.");
}
I felt that ResolveConflictingActions may potentially sweep a real issue under the rug.
I had two issues that caused the same error.
I have two classes with the same name under two different namespaces. Swagger could not reconcile this when generating the swagger doc. To fix it I added the line options.CustomSchemaIds(x => x.FullName);
See explanation here
I had a method without an [HttpGet] annotation. Swagger needs the HTTP endpoints to be explicitly defined.
I found both issues by inspecting the Output in visual studio after the API loaded.
I just spent two hours on this issue, but my cause was entirely different, it had NOTHING to do with routes or annotations. I had 2 classes with the same name (but different namespaces): MyProject.Common.ClassName and MyProject.Api.ClassName. Swagger/swashbuckle couldn't tell the difference between the two, so I got that useless error.
Those 2 hours were spent trial-and-error commenting out controllers and endpoints, to finally find 3 endpoints offending endpoints. All 3 endpoints had different routes, different (or no) custom authorization, and different method names. It turned out that all 3 endpoints either accepted a parameter, or returned an object, that contained the API version of my class. Nowhere was the Common version used. Swagger couldn't tell them apart, and puked all over itself.
Why oh why can't Swagger or Swashbuckle provide actual error messages? Would have saved me a couple of hours...
I just forgot to add HTTP attributes in my controller as soon as I add HTTP attribute it works like a charm for me.
Source : https://www.benday.com/2020/12/16/webapi-core-swagger-failed-to-load-api-definition-error/
Here we go:
I created WEB Controller instead of WEB API Controller. That makes this kind of error.
During creation of new Controller, make sure that you created right WEB API controller.
Surely it is one of the Controller's method that is faulty. To get the method, at times you might need to take out all your controllers, Try and insert them one after the other then you will test along to find the Controller with bugs.
For ex. If you have like 3Controllers say
>Controller
>>>AuthController
>>>UserController
>>>HomeController
Take two out of the controllers out and test the controller by adding one controller after each successful testing. With that you will know the controller that has a faulty method.
>Controller
>>>AuthController
If the methods in AuthenController is fine, It will run, If not Check the methods.
>Controller
>>>AuthController
>>>UserController
and carry out the next check on the controller like that of Authen.
I had the same problem, so I checked it using inspect element on the browser. The "Console" tab shows the file where the problem originated from (v1/swagger/json:1). Opening it by clicking it showed that one of the helper methods I used in my controller was "Public". Changing it to "Private" fixed the problem for me.
This page also has good tips:
https://btrehberi.com/swagger-failed-to-load-api-definition-fetch-error-undefined-hatasi-cozumu/yazilim/
Swagger in my case needed [HttpAction] with all public members in controller. Unfortunately I misspelled constructor name and since it was public, was throwing this error.
For ASP.NET Core 3.1 I had to ensure the verb were not ambiguous and I found this out by first running the API project without IIS in VS2019 (Green Arrow > left-click the carrot icon and select the name of the project this causes a console window to appear on start up so you can inspect what's happening and see errors).
[HttpGet("MyEndPointA")
Then Swagger is able to generate the documentation correctly.
Solved issue in dotNet 6! Just change the attribute order of [ApiController]
In my case, there were 2 methods in the Controller class, which had the same annotations, and URL. (Our team was using Entity Framework, ASP.NET and Swagger.)
[HttpGet("GetMyGreatData/{patientId}")]
[ValidatePatient]
public async Task<ActionResult<ServiceResponse<IEnumerable<MyGreatModel>>>> GetMyGreatData(
[FromRoute] int patientId, int offset = 0, int limit = 0)
{
//method details...
}
[HttpGet("GetMyGreatData/{patientId}")]
[ValidatePatient]
public async Task<ActionResult<ServiceResponse<IEnumerable<MyGreatModel>>>> GetMyGreatData(
[FromRoute] int patientId,
[FromQuery] DateTimeOffset? startdate = null,
[FromQuery] DateTimeOffset? endDate = null,
int offset = 0,
int limit = 0)
{
//method details...
}
deleting one method solved the issue for me.
I was having the same issue, the base controller was not decorated with Http and removing that has made it work.
This error can happen when you deploy an App Service to Azure. I've redeployed the App Service to Azure and the error disappeared.
When this happened to me, I tracked it down to URL path param having an underscore which it's compatible with the asp generator
Changing this:
/block-content/{machine_name}:
To this
/block-content/{machineName}:
Solved it for me
<b>make sure the name "v1" matches the path in the swagger endpoint</b>
<p>
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo {
Title = "ODAAPP",
Version = "v1" });
});
</p>
<br/>
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json",
"ODAAPP v1"));
enter code here
This will also happen if you use same route for multiple action methods (Overloading is OK)
In my case, the project was configured to authenticate using identity server 4 using AddPolicy() at startup.cs and there were usages of [Authorize]
I removed the things for startup.cs and usages of [Authorize]
Will update more soon
In my case I had two identicall inner classes.
Extracted them to a single one refactored the namespaces and voilá, all returned to work properly.
I have experienced the same error when I was using Swagger and also Microsoft.AspNetCore.OData. I usually try to use the latest version - but bringing it down to v 7.5.12 - did solve my issue.
Also adding following to every Action method in the Controller, makes it work with OData v8.x too: [HttpGet], [HttpPost], or [ApiExplorerSettings(IgnoreApi = true)]
I had a similar Fetch error 404 swagger/v1/swagger.json, when trying to integrate Swagger documentation in ASP.NET Core 3.1 Web API project. I tried almost all of the above suggestions but failed.
After an hour of hit-and-trial, I decided to give NSwag a try using this reference, instead of Swashbuckle and it just worked like a charm :)
I got the similar issues - the root cause is I forgot to add the annotations :-(
Reasons for this Error
i resolved this issue by this way
Use [HttpGet] attribute above the api controller method.
And, because of different versions of swashbuckle, these errors may come.
you should use the correct swagger endpoint url
v1/swagger.json or swagger/v1/swagger.json
choose above one based on the version you are using.
Note:
Use this url for reference https://myget.org/feed/domaindrivendev/package/nuget/Swashbuckle.AspNetCore.Swagger/6.2.3-preview-1963
Refer the official swagger documentation.
lot of information is there with crystal clear documents
https://swagger.io/docs/
'Multiple operations with path 'some_path' and method 'GET' '
[HttpGet]
public IActionResult Get()
{
return Ok(_userService.Get());
}
[HttpGet]
public IActionResult Get(int Id)
{
return Ok(_userService.Get(Id));
}
Just modify DataAnnotation:
[HttpGet]
public IActionResult Get()
{
return Ok(_userService.Get());
}
[HttpGet("{Id}"] //HERE
public IActionResult Get(int Id)
{
return Ok(_userService.Get(Id));
}

Making Asp.Net core route first and then making to React route next

I am new to react but not to asp.net core application development. I am trying to create a react application with asp.net core + react template in visual studio. I am trying to do a Asp.Net MVC route first to call controller action which has [Authorize] attribute to make sure user is authenticated. I do not have any thing to show in Asp.Net MVC view. I want to immediately redirect user to react default route once user is authenticated via asp.net mvc controller action. Is there any specific routing mechanism to achieve that.
Right now my application goes through controller and action based on route and stops at view defined by controller action. I am trying to understand how to redirect user to use react route now. I tried using return redirecttoaction and returnredirecttoroute but no luck.
My Asp.Net Core MVC Action
[Authorize]
public ActionResult Index()
{
var IsAuthenticated = HttpContext.User.Identity.IsAuthenticated;
var UserName = "Guest";
if (IsAuthenticated)
{
UserName = HttpContext.User.Identity.Name;
}
TempData["userName"] = UserName;
//return View();
return Redirect("my react first page");
}
I tried return Redirect("my react first page");
My Startup file method used for routing
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseAuthentication();
//MVC Part. I am trying this to authorize as FIRST step
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=DataAccess}/{action=Index}/{id?}");
});
// React part I am trying this to be called once Authentication is done in controller action. Trying to call this as a SECOND step
app.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
spa.UseReactDevelopmentServer(npmScript: "start");
}
});
}
If I do a redirect and force the react route will there be any issue missing react route features? I see this spa.UseReactDevelopmentServer(npmScript: "start"); is taking more time showing a timeout if I do a redirect. Any solution where User can be redirected to controller action do all authorization and use react default routing mechanism?
Is there any option to run react server first and do routing as starting server is taking more time leading to timeouts.
Note: I used Create-React-App to create my app.
Dont setup the same routes for both, let the server find the non React view, and let React have it own routes and view/templates
You should setup client side routing in react and server side routing in asp.net core.
Please note that client side routes are only limited to browser. They are not known to the server.
When you try to change page in react, browser (without sending request to server) redirects user to other page - provided you do not need any other information from server for this redirection.
In my opinion, you should design application in such a way that your server should not directly affect the routes defined in client side routing.
The ideal flow (again, in my opinion) for routing based on some decision on asp.net server would be:
The client calls server for some decision
Server sends response back to react
React then interprets response and decides whether to redirect or not.
If user is to be redirected, then where it should be redirected.
This logic (or any other similar logic) will also keep your server side logic completely decoupled from the client side technology.
I would not recommend doing any sort of routing or redirecting on the server side. This will complicate development, and also double the amount of work. The easier way is to handle routing or redirecting on the client side. This will be more maintainable from a code perspective as well.
From React, when the user logs in, call your controller action to do the authentication/authorization. You can then redirect appropriately with React depending on the response(e.g. a successful login redirects to the user's dashboard, a failed login displays an authentication error etc.)
I've worked through this problem with .net core and react as well.
React:
Set up an HOC for your routing. Have that hoc hit the backend to see if the user is authorized. If not redirect to login.
.Net Core:
Set up a basic path for the HOC to hit and verify the user is authorized.
Here's a complete write up on my github (although it is with jwt tokens): https://github.com/moh704/AuthenticationExample
//Routing with HOC:
class App extends Component {
render() {
return (
<Provider store={store}>
<ConnectedRouter history={history}>
<Switch>
<Route component={SignIn} exact path='/'/>
<PrivateRoute component={Home} path='/Home'/> //Token will have to be valid in order to access this route.
</Switch>
</ConnectedRouter>
</Provider>
);
}
}
//PrivateRoute Component:
interface PrivateRouteProps extends RouteProps {
component:
| React.ComponentType<RouteComponentProps<any>>
| React.ComponentType<any>;
}
interface State {
validatingUser: boolean,
userAllowed: boolean
}
class PrivateRouter extends React.Component<PrivateRouteProps, State> {
state: State = {
validatingUser: true,
userAllowed: false
};
componentDidMount(): void {
this.checkUserStatus();
}
checkUserStatus = async() => {
const token = localStorage.getItem('token');
if (token){
await axios.get(UserRoutes.GET_TOKEN_STATUS)
.then(() => this.setState({userAllowed: true, validatingUser: false}))
.catch(() => this.setState({userAllowed: true, validatingUser: false}));
} else
this.setState({userAllowed: false, validatingUser: false});
};
render() {
const { component, ...rest} = this.props;
return(
!this.state.validatingUser ?
<Route
{...rest}
render={props =>
this.state.userAllowed ? (
<this.props.component {...props} />
) : (
<Redirect // <---- **Redirect magic happens here you're aiming for**
to={{
pathname: "/"
}}
/>
)
}
/> : <div>loading...</div>
)
}
}
export default PrivateRouter;
For .net just create a simple get route that will return okay if authorized. Otherwise, it will return an unauthorized or forbidden:
[HttpGet]
[Authorize]
public IActionResult CheckUserState()
{
return Ok();
}
You can make the main react page a view it returns.
Then your controller can return that view or you can make a controller that will return that view and redirect there.

How do I set a default home page in ASP.NET Core 2?

I am having some difficulty configuring a default home page in an ASP.NET MVC Core 2 application. I seem to be unable to get the routing to map to the correct home page a the default when no parameters are used, ex. https://localhost:44362
I don't really like the default convention of putting Controllers in a Controllers folder, view in Views, and View Models in Models. I prefer to group by feature.
So I have a structure like this:
Features
Home
HomeController.cs
HomeIndex.cshtml
HomeViewModel.cs
Other
OtherController.cs
OtherIndex.cshtml
OtherViewModel.cs
Everything works fine, except I can't seem to get the default page to be /Home/Index when no path is provided. Everything worked when HomeController was in Controllers (and Index in Views/Home), but as soon as I moved it, things broke.
I'm using the default Startup.cs file, so I have a Configure method that looks like this:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
I did some searching it and it looks like using this in the ConfigureServices method should work:
services.AddMvc().AddRazorPagesOptions(options =>
{
options.Conventions.AddPageRoute("", "/Home/Index");
});
That throws a: Value cannot be null or empty (pageName) exception. I also tried "/", "/Home/Index" as parameters. Finally, just for good measure, I also swapped the above parameters just in case I was misunderstanding usage of each parameter.
In every case, no page is displayed. If I enter the full URL /Home/Index, it works. I also tried to attach a [Route("Home")] attribute to the HomeController class and a [Route("Index")] to the action method.
So I'm obviously messed up the routing, but it's not at all clear to me how to fix this. I've read and re-read the docs on Razor Pages which doesn't seem to clear up my understanding.
#paul-mrozowski, you were almost correct in your solution above. For basic pages you'll have to use the following approach to change the default route, provided you Index page is under /Pages/Home/ directory:
services.AddMvc().AddRazorPagesOptions(options =>
{
options.Conventions.AddPageRoute("/Home/Index", "");
});
Just in case someone is using Areas, an approach would be similar, provided your Index page is located under /Areas/SomeArea/Pages/ directory:
services.AddMvc().AddRazorPagesOptions(options =>
{
options.Conventions.AddAreaPageRoute("SomeArea", "/Index", "");
});
It looks like all I needed to do was adjust the locations the Razor view engine looks in my Startup.cs
How to specify the view location
and here in this blog post:
Feature Folder Structure in ASP.NET Core
Then I just needed to remove my Route attributes I had added during testing and it started working.

MVC6 Strongly typed action links and views

We are currently getting a new application stood up with MVC6. In previous versions, we used T4MVC so we could do stuff like:
#Url.Action(MVC.Home.Index);
and
return View(MVC.Home.Views.Index, model);
In the new application I have to use magic strings. I Hate magic strings. Are there any alternatives for this for MVC6?
Came across this AspNet.Mvc.TypedRouting Repository on GitHub that I thought would be useful when I eventually move over to MVC6.
Not sure if it handles views as well though
Some instructions from the readme
To use expression based link generation, you need to do the following into your Startup class:
public void Configure(IApplicationBuilder app)
{
// other configuration code
app.UseMvc(routes =>
{
routes.UseTypedRouting();
});
}
Basically, you can do the following:
// generating link without parameters - /Home/Index
urlHelper.Action<HomeController>(c => c.Index());
// generating link with parameters - /Home/Index/1
urlHelper.Action<HomeController>(c => c.Index(1));
// generating link with additional route values - /Home/Index/1?key=value
urlHelper.Action<HomeController>(c => c.Index(1), new { key = "value" });
// generating link where action needs parameters to be compiled, but you do not want to pass them - /Home/Index
// * With.No<TParameter>() is just expressive sugar, you can pass 'null' for reference types but it looks ugly
urlHelper.Action<HomeController>(c => c.Index(With.No<int>()));

Categories