Call MVC method from Controller Full Path - c#

I have an issue because I do not know how to call a specific method inside a Controller that is far away on the foler structure. I try to call it this way from my View
#Html.Action("Method", "DesiredControllerName", parameter)
I am pretty sure this works, because I am calling other methods from controllers this way. The only problem is that this time I need to reach a controller that is way up in a different folder, i.e.
The controller is in:
MySolution/Areas/Area1/Controllers/DesiredControllerName
Whereas the CurrentView Im making the call is in:
MySolution/Areas/Area2/Views/Client/CurrentView
How can I make the call work?
Thanks!
EDIT:
I found it,
As the controller is in another Area I just had to add it as a parameter
The controller for path was not found or does not implement IController

looks like you maybe got it, where your example code line:
#Html.Action("Method", "DesiredControllerName", parameter)
has the 'parameter' parameter, try using an array of anonymously typed routevalues and include an area and controller, something like this:
#Html.Action("Method", new { Area = "Area1", Controller =
"DesiredControllerName", })
I am used to using this style of providing mvc routing info using Html.ActionLink, but the Html.Action should be similar-ish. here is the msdn page for the Action method, hope that helps

#Html.Action("Method", "DesiredControllerName", new { Area = "Area1" })
This will works

Related

#Url.Action("Action","Controller") calls both post and get method with same name

I have a controller where there are two action methods with same name with HttpPost and HttpGet as shown below , when link with #Url.Action("DoSomething","Controller") is called then both the method are invoked , how do I call only GET method?
Is there anyway to pass type GET or POST in #URL.Action or any other way?
[HttpGet]
public ActionResult DoSomething(int ? MyParam)
{
}
[HttpPost]
public ActionResult DoSomething(MyModel Model)
{
}
In Razor View When I build a link like
JustDoIt
Then it calls both POST and GET methods, even after specifiying parameter as int and not the model
Try adding post in the form declaration:
#using (Html.BeginForm("DoSomething","Controller", FormMethod.Post))
{
}
or using HTML:
<form action="#Url.Action("DoSomething","Controller")" method="POST">
...
</form>
The HTTP verb POST/GET will route your request to the appropriate action. The default is GET.
I think that you maybe getting Url.Action() confused with Html.Action() (apologies if I'm wrong). As someone mentioned in the comments, Url.Action() will just render a URL based on the parameters which is intended for say building anchor tags. Html.Action() on the other hand will invoke an action when building the page for the response at server side which I'm guessing is what your referring too.
If that's the case, then you will need to specify the Action's parameters for DoSomething() by specifying the route values like so:
#Html.Action("DoSomething", "Home", new {MyParam = 2} )
The result of DoSomething(int MyParam) would be injected into the page. Html.Action is not intended to call a POST so it's not possible to use this on an Action which has been decorated with the POST verb.
Issue was I was having one more ajax request to check if the page has been loaded completely to show progress bar, when removed that portion of the code from layout, It has been working fine without calling any action method twice.

Object reference not set to instance of an object, While using Url.Action() method

I am getting error: object reference not set to an instance of an object in this line of code in my Controller class method:
var ChangeEmailUrl = Url.Action("ChangeEmailConfirmation", "ManageAccount");
My objective is to get a URL path to an action 'ChangeEmailConfirmation' in controller 'ManageAccount'. The string link returned by the Url.Action() method would be sent in an email.
Also, I would like to know if there is any other way to get URL link. Any light on solving this problem would be appreciated.
If you wanted to get an absolute url
(http://localhost:8385/MyController/DoThis):
var ChangeEmailUrl = Url.Action("DoThis", "MyController",null,Request.Url.Scheme,null)
See also
you can try with urlhelper or htmlhelper
Url Helper:
Contains methods to build URLs for ASP.NET MVC within an application.
Official doc:https://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper(v=vs.118).aspx
HtmlHelper.GenerateLink Method:
Generates an HTML anchor element (a element) that links to an action method
https://msdn.microsoft.com/en-us/library/system.web.mvc.htmlhelper.generatelink(v=vs.118).aspx
Example:
https://stackoverflow.com/a/700357/3397630
Hope it was useful
Thanks
Karthik

Generate an url in ASP.NET MVC outside a view or controller

I follow the article "Simulate a Windows Service using ASP.NET to run scheduled jobs" on CodeProject, and in the part "Store item in cache again upon expire" I need to hit a page of my application.
Instead of hard writing the url, I wanted to generate it with the Asp.Net MVC UrlHelper but it seams that it required some instances that I don't have access in this context, because it's not in a Controller or a View.
Is it possible or is there another solution?
EDIT:
Sadly HttpContext.Current is not accessible in CacheItemRemovedCallBack.
So the only solution appears to store the value needed (Url.Host) in the Application_Start method in order to build the full url later.
You can use Url.Action method with the following parameters:
#Url.Action("Index", "Home", routeValues, Request.Url.Scheme)
or
new UrlHelper(HttpContext.Request.RequestContext).Action("Index", "Home", routeValues, Request.Url.Scheme)
You can create your own instance of UrlHelper.
For this, you need to have an access to current http request.
This could be achieved using static instance of HttpContext : HttpContext.Current:
new UrlHelper(HttpContext.Current.Request.RequestContext).Action("action");
[https://www.codemag.com/Article/1312081/Rendering-ASP.NET-MVC-Razor-Views-to-String][1]
check this for complete description how to do this

parameters in url in asp.net mvc

I know this can be done, but don't even know what its called to find a good tutorial from Google. I am using ASP.Net MVC4 and I have a controller called ePage, right now I can access what I want from a URL like this
http://www.myUrl.com/ePage/{ACTION}/{PARAMETER as "id"}
how can I change the routing so that (just for this controller if possible) it is read like this
http://www.myUrl.com/ePage/{PARAMETER}
I will always be using "Index" as Action for now.
If there is a simple answer to do that'd be awesome , if not just a point to the right direction for me to read and figure out.
In your Global.asax.cs under the RegisterRoutes method, you can try adding:
routes.MapRoute("MyNewRoute", "ePage/{param}", new {
controller = "ePage",
action = "Index",
});
Your Index method must have an argument named param so that the routing will match.

ASP.NET MVC public alternative to UrlHelper.GenerateUrl

I want to embed a link to a controller action in my page so I can use it from javascript. Something like
var pollAction = '/Mycontroller/CheckStatus'
Now I am happy to hardcode it, but it would be really nice if there were a method I could use to create the URL. The AjaxHelper/HtmlExtensions contain methods to create hyperlinks (.ActionLink(...) and so on), but if you look into the guts of them, they rely on a method called UrlHelper.GenerateUrl() to resolve a controller and action into a url. This is internal so I can't really get at this.
Anyone found a good method in the framework to do this? Or must I roll my own?
Have you tried something along these lines?
var pollAction = '<%=Url.Action("CheckStatus", "MyController") %>';
If your page or control inherits from ViewPage or ViewUserControl, use the Url.Action method.
If not, use this instead:
String url = RouteTable.Routes.GetVirtualPath
(
((MvcHandler) HttpContext.Current.CurrentHandler).RequestContext,
new RouteValueDictionary
(
new
{
controller = "MyController",
action = "CheckState",
id = idParameter
}
)
).VirtualPath;
Place this inside a method on your code-behind and call it from the HTML view.

Categories