Ajax.BeginForm absolute url MVC CORS - c#

Is there any way to get the Ajax.BeginForm to output a absolute url rather than just a relative one?
I am trying to get some forms working with CORS on a site hosted on another server.
At the moment I am parsing the form with jquery onsuccess in the ajax options.
I am not any pointers to a tutorial on writing an extension for it would be handy

Is there any way to get the Ajax.BeginForm to output a absolute url
rather than just a relative one?
Yes, but you need to specify the Url in the AjaxOptions and not use the overload which takes action and controller names:
#using (Ajax.BeginForm(new AjaxOptions { Url = Url.Action("Foo", "Bar", null, "http") }))
{
...
}
Url.Action has an overload where you could specify the protocol to be used which generates absolute urls. And if you wanted to use the same protocol as the one used to request the current action instead of hardcoding it you could use the following:
#using (Ajax.BeginForm(new AjaxOptions { Url = Url.Action("Foo", "Bar", null, Request.Url.Scheme) }))
{
...
}

Here:
#using (Ajax.BeginForm(new AjaxOptions {
Url = "http://www.domain.com/whatever" ,
Confirm = "...",
// and other options...
}))
{
// html controls or helpers here...
}

Related

ActionLink and relative path

I can't find out how to solve this. I have two URLs. These are /my-url-1 and /my-url-2. Both going to different views.
The thing is that I have an ActionLink on /my-url-1's view which should make /my-url-2 and go to that view.
The problem is that ActionLink makes /my-url-1/my-url-2 as the URL and not just /my-url-2.
I was searching two days about how to fix it but couldn't find anything.
PD: I'm not using Areas so please don't tell me that I just should put the "area" parameter as a "".
These are two urls which goes to different controllers and different actions.
View which has the ActionLink (URL:/my-url-1) :
<div class="btn-index-container">
#Html.ActionLink("Url 2", "MyAction", "MyController")
</div>
This ActionLink should render:
Url 2
But it's rendering:
Url 2
where /my-url-1 is my current URL
Route Config
routes.MapRoute(
name: "route1",
url: "my-url-2", //without parameters
defaults: new { controller = "MyController", action = "MyAction" },
);
routes.MapRoute(
name: "route2",
url: "my-url-1", //without parameters too
defaults: new { controller = "MyController2", action = "MyAction2" }
);
So, when I go to localhost:port/my-url-1 it loads MyAction2 which renders a view. This view has inside an ActionLink(described above) which should render a /my-url-2 link.
Well, I've worked inside the MVC framework and I could told you about how Url.RouteUrl or Html.RouteLink works. At the end, the method which create the URL is GetVirtualPathForArea (this method is called before UrlUtil.GenerateClientUrl, which receive the VirtualPathData.cs created by GetVirtualPathForArea, as a parameter) from System.Web.Routing.RouteCollection.cs.
Here I left a link to the MVC source code:
https://github.com/aspnet/AspNetWebStack/blob/master/src/System.Web.Mvc
I found that, my Request.ApplicationPath was changing when I loaded /my-url-1. It was crazy because the application path was /.
At last, the problem was that the /my-url-1 was pointing to a virtual directory created on the IIS some time ago by error.
To know where your IIS configuration file is, please follow the link below:
remove virtual directory in IIS Express created in error
The solution was remove the .vs directory (which contains the config .vs\config\applicationhost.config) and rebuild
I think most of the Helpers that render URLs works more or less in the same way, so I hope it'll useful for all of you!
In your case, maybe no its necesary, just pass the parameters with null values, E.G.
#Html.ActionLink("EspaƱol", null, null, new { Lng = "es" }, null)
In this way, the parameters change, and the view is relative, depending on where you are.

MVC Action returning either Actionresult leaving the view unmodified or a View actionresult

My problem in an MVC Razor application:
I use a polling controller action call based on a javascript timer to request if there is already an answer (which by the way happens through WCF in the Controller call).
If there is an answer, the view gets updated through a normal return View(...) action result, which updates the view correctly.
But if the answer is still not available, I would like an ActionResult that does not change or update the View. I tried to return EmptyResult, but then my webpage turns white. I would like not to modify my controller action call method if possible:
#using (Html.BeginForm("actionTryGetNewSystemSentence", "Conduct", FormMethod.Post,
new { onsubmit = "OnTryGetNewSystemSentence(event)", id = "idConductForm" }))
{ ...}
I also tried other ActionResult types but either they modify the view or return a blank or invalid View. The polling gets done every 500 ms, so I can't let the view update every 500 ms.
You are using here a Html.BeginForm which mean it is not a ajax call. You must specify Ajax.BeginForm and define a function in jquery that you will call when the result is success or fail and update the html accordingly.
#using (Ajax.BeginForm("actionTryGetNewSystemSentence", "Conduct", new AjaxOptions { HttpMethod = "POST", onsubmit = "OnTryGetNewSystemSentence(event)", OnSuccess = "Successmethod(event)", OnFailure = "Failuremethod(event)", id = "idConductForm" })) { ...})
check the link below
http://www.c-sharpcorner.com/UploadFile/0c1bb2/ajax-beginform-in-Asp-Net-mvc-5/

How to pass anti forgery token to react via asp.net mvc?

I call my jsx file via the cshtml like so:
#model viewmodels
#Html.React("CreateStuff", new {
options = Model.KeyValues})
My jsx file contains a form which I'd like to POST back to my controller. However, the controller has a ValidateAntiForgeryToken attribute. How can I pass in the token from mvc land to react??
You should to include in your view this row:
#Html.AntiForgeryToken()

C# ASP MVC : navigate between different controllers views from HTML

I have a problem navigating between different controllers views from HTML
Like for example i have two controllers (User and Transaction)
and in my HTML there is a main menu where it has all the main navigations.
so if i wanna navigate to the User list my view would be "User/List_Users"
and if i am inside the Transaction view
(......com/Transaction)
if i clicked on User List it will navigate to
(......com/Transaction/User/List_Users)
instead of going to
(......com/User/List_Users)
So i tried using The Html Action like like
<li>#Html.ActionLink("User List","User/List_Users")</li>
but didn't do any good :(
<li>#Html.ActionLink("Link Name", "Action")</li>
This is your basic ActionLink. An action is the specific method in the controller (which ultimately serves up a view).
If you need to link to a different controller (you need to link to a Transaction view from a User view, for example), you can do:
<li>#Html.ActionLink("Link Name", "Action", "Controller")</li>
Use the overload which accepts controller name:
#Html.ActionLink("User List","List_Users", "User");
I usually use the following
#Html.ActionLink("Link Text", "Action", "Controller", new { querystringparameter = querystringvalue }, null)
In your case it will be:
#Html.ActionLink("User List", "List_Users", "User", new { querystringparameter = querystringvalue }, null)

ASP.NET MVC 3 Url.Action matching

I have the following two routes defined:
routes.MapRoute(null, "" // ~/
,new { controller="Products", action="List", page=1 });
routes.MapRoute(null, "{category}/Page{page}" // ~/CategoryName/Page21
, new { controller = "Products", action = "List", page = 1 }
, new { page = #"\d+" } //page must be numerical
);
I am generating a URL using this code in the view being used by ProductsController: Url.Action("List", new {page=p, category=Model.CurrentCategory})
With the current configuration, I get this URL: /Riding/Page2
However, if I omit the default page parameter from the first route, I get this URL instead: /?category=Riding&page=2
It seems to me that Url.Action() can match both routes and is making a decision to use the second route if I have the default page parameter specified in the first route but is choosing to use the first route if that parameter is omitted.
Given that I am supplying a value for page parameter, why would eliminating it from the route's defaults make a difference in the URL I get back?
Thanks!
Try installing the NuGet package Glimpse. It has excellent route debugging, it should be able to help you.
Here's a blog post by Scott Hanselan on how to use it: NuGet Package of the Week #5

Categories