I have an url like this:
http://localhost:9562/Account/LogOn?ReturnUrl=%2fCabinet%2fCabinet
I need to parse it to this:
Cabinet/Cabinet
I've looked through
this and this but i can't understand how to use it with my example.
The easiest way would be to accept it as a parameter in your LogOn action:
public class AccountController : Controller
{
public ActionResult LogOn(string ReturnUrl = "")
{
}
}
Note, providing a default value (i.e. = "") allows the action to execute even if the query parameter isn't present in the request.
Alternatively, you could access it through the Request property of your controller:
public class AccountController : Controller
{
public ActionResult LogOn()
{
string request = this.Request.QueryString["ReturnUrl"];
}
}
Try this:
string r = Request.QueryString["ReturnUrl"].Substring(1);
Response.Write(r);
Related
I have Web API, I have written action method. but it's not correctly visible when I run the application. I cannot see SendPushNotification in the attached image.
Controller Code:
[RoutePrefix("api/OTP")]
public class OTPController : ApiController
{
public IHttpActionResult Get(int id)
{
return Ok("value");
}
[HttpGet]
public IHttpActionResult SendPushNotification(string userId, string factorId, string domain)
{
var response = _oTPRepository.SendPushNotification(userId, factorId, domain);
return Ok(response);
}
add a Route over your method, something like this:
[HttpGet]
[Route("SendPushNotification")]
public IHttpActionResult SendPushNotification(string userId, string factorId, string domain)
{
var response = _oTPRepository.SendPushNotification(userId, factorId, domain);
return Ok(response);
}
This will combine with the RoutePrefix from your controller and give you what you want.
You can call it whatever you want as well, whatever makes sense for your API.
in mentioned image second method is that for which you are actually looking for.
default routing for action methods is api/{controller_name}.
if you want to access that method as your given name you have set routing attribute above that action method.
like [Routing("api/OTP/SendPushNotification")]
How to create route like site.net/query?
Attention, not
site.net/?q=querystring
What I want is:
site.net/querystring
I tried something like this, but this don't work
[Route("")]
class MyController : Controller
{
[HttpGet]
public string Get()
{
return Request.QueryString.ToString();
}
Just put that path in the routing string:
[HttpGet("/Something/[action]/{q}")]
public string Get(string q)
{
return q;
}
When you access this link: localhost/Something/Get/mynicestring
mynicestring will be shown
I develop a WebApi RESTful in ASP.NET Core 2.0.
From an action method in a controller A I want to create the URL address for an action method located in a different controller B:
[Route("api/applications/{idbcon}/bcontrollers")]
public class BController : Controller{
[HttpGet({idbcon}), Name = "ActionB"]
public IActionResult ActionB(int idbcon, [FromHeader(Name = "Accept")] string mediatype){
return OK();
}
}
[Route("api/applications/{idapp}/bcontrollers/{idbcon}/acontrollers")]
public class AController: Controller{
[HttpGet(), Name = "GetURLBController"]
public IActionResult GetURLBController()
{
var url = /* Here I would like to get the link to "ActionB" that belong to a Controller "BController" */;
return Ok(url);
}
}
Any advice ???
Use the IUrlHelper.Action overload that takes action and controller parameters:
public class AController : Controller
{
[HttpGet]
public IActionResult GetURLBController()
{
var url = Url.Action("ActionB", "BController");
return Ok(url);
}
}
IUrlHelper is something you inherit from the Controller base class, exposed as the Url property.
I am defining my application URLs like
domainname.com/storecontroller/storeaction/storename
I want to rewrite like
domainname.com/storecontroller/storename
Actually, I need to skip storeaction from the url, I don't want to do it with querystring with "?" Is there anyway to do it by registering rout config path or any another way?
Yes. You can define action as a default parameter, and match for only specific controllers with a Regex constraint:
routes.MapRoute("<route name>", "{controller}/{storename}",
new
{
action = "storeaction"
},
new
{
controller = "somecontroller1|somecontroller2|somecontroller3",
});
(Action will always have the default value "storeaction")
Note that you need to define this route before the default generic route so it doesn't catch it before this kicks in.
Using Attribute routing
[RoutePrefix("Home")]
public ActionResult HomeController : Controller
{
[Route("{storeName}")]
public ActionResult ProcessStore(string storeName)
{
// to do : Return something
}
public ActionResult Index()
{
// to do : Return something
}
}
[RoutePrefix("Payment")]
public ActionResult PaymentController : Controller
{
[Route("{storeName}")]
public ActionResult ProcessStore(string storeName)
{
// to do : Return something
}
}
If I have this action:
public ActionResult MyAction(long? personId)
{
// ...
}
I can call that action with this URL:
localhost/MyAction/?personId=x
I want to be able to send another extra parameter (token), which is going to be in all of my ajax calls from the client:
localhost/MyAction/?personId=x&token=asdf
Without having to declare this new parameter in all of my action signatures. That it: I want to avoid this:
public ActionResult MyAction(long? personId, string token)
{
// ...
}
public ActionResult MyAction2(long? animalId, string token)
{
// ...
}
etc.
But I also want to be able to have access to the token parameter from inside the method. That is:
public ActionResult MyAction(long? personId)
{
// I can check the token's value or manipulate it, eg:
if (token.Equals(...)) { .. }
}
Question:
Is there a way to declare this parameter implicitly in all (or some) of my actions? Maybe using attributes?
You could derive all your controllers from a common base controller.
Then, override OnActionExecuting in that and access the Querystring. For example:
public class BaseController : Controller
{
protected string Token { get; private set; }
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
string token = Request.QueryString["token"] as string;
Token = token ?? string.Empty;
}
}
I find that a cleaner solution than a static class. Also, the BaseController is a useful pattern in MVC to share code that is used in many controllers.
public class Token
{
public static string Value
{
get
{
return System.Web.HttpContext.Current.Request["token"];
}
}
}
public ActionResult MyAction(long? personId)
{
// I can check the token's value or manipulate it, eg:
if (Token.Value.Equals(...)) { .. }
}
Do not forget to account for Token.Value being null.