How to get #Url.Action value inside a controller - asp.net-core

I am using ASP.net core
I can use an Html action inside a view
#Url.Action("GetOptions", "ControllerName", new { id="1"});
However I want to get a string value of it in the Controller.
e.g. something like
string Url= Url.Action("GetOptions", "ControllerName", new { id="1"}).ToString();
In previous versions of MVC you can reference the helper in the controller by
UrlHelper urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
Basically what I want to do is generate a URL string representation in my controller

In order for the route values to work correctly for me I had to use the static invocation of the url helpers
UrlHelperExtensions.Action(Url, "Details", "Resellers", new { id = 1 })
Edit: The shorthand way of writing this is:
this.Url.Action("Details", "Resellers", new { id = 1 })
Thanks #Learner.

Related

How create new route with interface IEndpointRouteBuilder?

We have upgraded project to asp.net core 3.0 and would like to use Endpoint routing.
I have following old method:
public static IRouteBuilder MapLocalizedRoute(this IRouteBuilder routeBuilder,
string name, string template, object defaults, object constraints, object dataTokens)
{
if (routeBuilder.DefaultHandler == null)
{
throw new ArgumentNullException(nameof(routeBuilder));
}
//get registered InlineConstraintResolver
var inlineConstraintResolver = routeBuilder.ServiceProvider.GetRequiredService<IInlineConstraintResolver>();
//create new generic route
routeBuilder.Routes.Add(new LocalizedRoute(routeBuilder.DefaultHandler, name, template,
new RouteValueDictionary(defaults), new RouteValueDictionary(constraints),
new RouteValueDictionary(dataTokens), inlineConstraintResolver));
return routeBuilder;
}
I need change to type this IEndpointRouteBuilder routeBuilder.
But how me implement adding of new generic router? Interface IEndpointRouteBuilder dosent have property Routes and I can't use routeBuilder.Routes.Add...

Netcore 2.2 Localized Routing - Route values for default culture always ignored

Successfully using the project laid out at Localized routing using ASP.NET Core MVC 2. The project is developed to display the Default language using just the controller/Action and Localized Routes for the alternate languages.
My Controller
[LocalizationRoute("en", "Portal/Dashboard")]
[LocalizationRoute("fr", "Portail/tableau-de-bord")]
[LocalizationRoute("es", "Portal/Tablero")]
public class DashboardController : PortalBaseController
{
private const string Title = "Dashboard";
[LocalizationRoute("en", "Dashboardv1")]
[LocalizationRoute("fr", "tableau-de-bordv1")]
[LocalizationRoute("es", "Tablerov1")]
public IActionResult Dashboardver1()
In my SignIn controller I wish to create a localised RedirectUrl...the below code provides a fully localized route for the the french and spanish but the default route is always just controller/action. If I manually type in the english, fully localized route, the page loads but the code below always returns just a regular path.
var culture = CultureInfo.CurrentCulture.Name;
var redirectUrl = LocalizationRouteDataHandler.GetUrl("Dashboard", "Dashboardver1", culture).Url;
//should be "en/portal/Dashboard/Dashboardv1"
//currently resolves to "Dashboard/Dashboardver1"
Suggestions as ?
I find that In LocalizationRouteDataHandler.AddControllerRouteData ,code ControllerRoutes[controllerKey].Names.TryAdd(culture, route); adds culture="en" and route="Dashboard" first to the Dictionary of Controller Name and then when it tries to add the condition culture="en" and route="en/Portal/Dashboard",it fails since the "en" key has added.
A workaround is that you could remove the unnecessary key and add the new key.
LocalizationRouteDataHandler.cs
public static void AddControllerRouteData(string controller, string culture, string route)
{
if (controller == "Dashboard")
{
Console.WriteLine("test");
}
string controllerKey = controller.ToLower();
// If the controller doesn't exist, create it!
if (!ControllerRoutes.ContainsKey(controllerKey))
{
ControllerRoutes.TryAdd(controllerKey, new CultureControllerRouteData());
}
// key removed
if (culture=="en" && ControllerRoutes[controllerKey].Names.Remove("en"))
{
ControllerRoutes[controllerKey].Names.TryAdd(culture, route);
}
// dictionary doesn't contain the key
else
{
ControllerRoutes[controllerKey].Names.TryAdd(culture, route);
}
}
When I test with
var redirectUrl = LocalizationRouteDataHandler.GetUrl("Dashboard", "Dashboardver1", "en").Url;
It returns /en/Portal/Dashboard/Dashboardver1

Add all query strings on controller action

I have a simple mvc4 application. An action link opens a view with bunch of query string parameters. The view contains a simple form when you click on submit button it posts the form and comes back to the view but I lost the query strings. what I must to do to have same query strings even after you have submitted the form?
One possibility is to specify the current request as action attribute of your form:
<form action="#Request.Url.AbsoluteUri" method="post">
...
</form>
But this will POST to the same resource. If you want to specify a different controller and/or action you could write a custom BeginForm helper which will do the job.
Something along the lines of:
public static class FormExtensions
{
public static IDisposable MyBeginForm(this HtmlHelper html, string controller, string action)
{
var builder = new TagBuilder("form");
var urlHelper = new UrlHelper(html.ViewContext.RequestContext);
var routeValues = new RouteValueDictionary();
var query = html.ViewContext.RequestContext.HttpContext.Request.QueryString;
foreach (string key in query)
{
routeValues[key] = query[key];
}
builder.MergeAttribute("action", urlHelper.Action(action, controller, routeValues));
builder.MergeAttribute("method", "POST", true);
html.ViewContext.Writer.Write(builder.ToString(TagRenderMode.StartTag));
var form = new MvcForm(html.ViewContext);
return form;
}
}
and then:
#using (Html.MyBeginForm("myaction", "mycontroller"))
{
...
}
This will effectively keep the current url query string parameters.

writing route in mvc4 to eliminate query string

I have a situation where I am redirecting to an action that accepts 3 parameters. This I am doing like -
RedirectToAction("ProductSpecific", routeValues: new { partId = m.partId, categoryId= m.categoryId, categoryName = m.categoryName});
However, when the page loads, it contains all these parameters as query string.
Parts/ProductSpecific?partId=38&categoryId=1&categoryName=Monitor
I tried writing a route, but that didn't work. Can someone please guide on how to write a route in this scenario?
Thanks
The second argument of RedirectToAction is routeValues, so these will be appended to the querystring. Creating an extra route will still require you passing the values in the querystring, but like this: parts/productspecific/{partId}/{categoryId}/{categoryname} which i dont think you want.
If you dont want the values in the querystring, have a look at the TempData object, which is similar to session but will live until the next request.
Something like this:
public ActionResult DoSomething()
{
TempData["partId"] = partId;
TempData["catId"] = catId;
TempData["catName"] = catName;
return RedirectToAction("ProductSpecific");
}
public ActionResult ProductSpecific()
{
var partId = TempData["partId"];
var catId = TempData["catId"];
var catName = TempData["catName"];
var model = service.LoadProduct(partId, catId, catName);
return View(model);
}
Update:
For a route:
routes.MapRoute(
name: "ProductRoute",
url: "{controller}/{action}/{partId}/{categoryId}/{categoryname}",
defults: new { controller = "product", action = "productspecific"}
);
Add that route in the route.config class in app_start before your default routesm, and change your product specific method signature to accept the partid, catid and category name parameters. You can also use this from phil hack to profile your routes: Route Debugger

asp.net mvc, generate url by custom route

i have created application, where url generates depends on database values.
i parse these urls without any problem and get controller and action from database in my route handler.
but when i try to generate url, i get troubles.
in my case, it seems like:
view
#Html.ActionLink("more", MVC.Blog.Post(item.Alias)) // i use T4MVC
MyRouteConstraint
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
if (routeDirection == RouteDirection.UrlGeneration)
{
var data = GetDataFromDbByControllerActionAndParameters(values);
if (data == null)
return false;
var valuesToRemove = new List<string>();
var path = GenerateUrlByData(data, valuesToRemove);
values.Remove("controller");
values.Remove("action");
valuesToRemove.ForEach(v => values.Remove(v)); // remove values that is already used in path
values.Add("all", path) // path = "blog/post/postalias"
return true;
}
// skipped code
}
route rule
routes.MapRoute("Locations", "{*all}",
constraints: new { all = new LocationConstraints() },
defaults: new { },
namespaces: new []{typeof(BaseController).Namespace}).RouteHandler = new LocationRouteHandler();
and as result i got url like this
localhost:8553/?Controller=Blog&Action=Post&alias=postalias
but expect like this
localhost:8553/blog/post/postalias
how can I generate url? where it should be? i think not in the constrant, but why it is invoked in this case?
In my MVC application the closest route that matches the one you have is the one below:
routes.MapRoute(
name: "MyRouteName",
url: "{SomeFolder}/{SomePageName}",
defaults: new { controller = "MyController", action = "Index" },
constraints: new { routeConstraint = new MyRouteConstraint() }
);
SomeFolder can be fixed or changed from the database while SomePageName will be changed to a different value from database. The url should be whatever URL you want to match with this route and the one that will be replaced by the value from the database. The defaults address the Controller and Action that will render the page in the end of the MVC cycle. The constraints will lead to your Match method described.
With this configuration I have URLs like www.project.com/SomeFolder/SomePageNameFromDatabase.