Open New tab from code behind in Asp.net MVC - asp.net-mvc

I need to open another site in new tab from code behind in Asp.net MVC.
return Redirect("Url"); is used to open the another site within the same tab.

It doesn't really seem practical for the users, because after authenticating in the second tab, they have to refresh the first tab to see the effects.
The ReturnUrl property of FormsAuthentication seems to do what you want. When the user needs to log in, they are redirected to the login page, and after signing in they are redirected back.
If you are making extensive use of javascript and ajax, and want to keep the javascript variables of the current page but need to log in to do the ajax calls, there might be another solution. If the response of your ajax call is the unauthenticated header, open a lightbox or something like that with a username and password field. Use ajax post to the AccountController to sign in the user again. This way, the user is authenticated again, but you keep the javascript variables.

This can be done using javascript only. Try this.
<%
Response.Write '<script type="text/javascript">
window.open(url);
</script>'
%>
Hope it works.

if you call action from form and use input type of submit you can try
<input type="submit" formtarget="_blank" />
if you use link <a> or AjaxCall you can try
<a target="_blank"></a> or in ajax helper set property #target="_blank"
here is my code
cshtml
#using (Html.BeginForm("PersonsReport", "Reports"))
{
<br />
<div style="text-align: center;">
<input type="submit" formtarget="_blank" class="btn btn-primary" value="GetReport" style="width:100%;" />
</div>
<br />
}
controller.cs
public ActionResult PersonsReport()
{
return Redirect("/PersonsReport.aspx");
}

Related

MVC CSRF AntiForgeryToken never seems to expire. Is there a way to invalidate token after each session?

I have an MVC 4.0 site that uses AntiForgeryTokens to protect against CSRF attacks.
What I am finding is that I can reuse an old __RequestVerificationToken after new sessions have been opened.
I start with adding to the .cshtml
#Html.AntiForgeryToken()
I then decorate the Post Action method in the controller with
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult DoSomething(MyViewModel model)
{
}
I then do the following :-
Log in to admin site and scrape the __RequestVerificationToken value.
Log Out
Log Back In again (in theory there should now be a new token created)
Then submit the form below in a new tab in the same browser (see that I have used the token from the previous request).
<html>
<body>
<form action="http://adminsite.com/DoAction" method="POST">
<input type="hidden" name="id" value="26" />
<input name="__RequestVerificationToken" type="hidden" value="rt95zr0voZbgLga117YNBfwwLpTU8onGCDmZ4IQEisvhiNH_9ISTtsbDzIVgIkRUzwH81PpbrTRGK4MLSp3S3j-JMNjsJTL04TRl2J38rNz8KKomL98gLjEiJoXgMXFt0qaJ8tPaB4_PvGo8ATaxLcA2" />
<input type="submit" value="Submit request" />
</form>
</body>
</html>
What I find is that even though I have logged out and back in again, I am still able to use the old __RequestVerificationToken and I can successfully post the form.
After reading through the documentation (http://www.asp.net/mvc/overview/security/xsrfcsrf-prevention-in-aspnet-mvc-and-web-pages) there is no mention of invalidation of token.
My questions are :-
Shouldn't it now be invalid?
If not, why not?
Is there a way of making the __RequestVerificationToken invalid after logging out and back in again?

how to make a submit button redirect to another website when submiting?

I have a HTML page. I have a submit button on the page. When user clicks the button, I want to redirect him to another site. Where should I put the code? Please be specific.
Thanks for advance !
Try something like
<form action="http://myRedirectedSite.com/redirectedPage.html" method="get">
<input type="submit" value="Submit">
</form>
You could use either get or post as the method depending on the other site. Note that any form field values will be passed to the other site.

MVC - Input submit causing redirect automatically

I have the following form
<form id="file_upload" action="/Upload/Save" method="POST" enctype="multipart/form-data">
<input type="text" id="txtProposalName" name="name" placeholder="Nome da Camiseta" />
<input type="text" id="txtProposalDesc" name="description" placeholder="Descrição da Camiseta"/>
<div class="fileupload-buttonbar">
<div class="progressbar fileupload-progressbar"></div>
<span class="fileinput-button">
Upload images
<input type="file" name="files[]" multiple />
</span>
</div>
<input type="hidden" id="imgID" name="imgID"/>
<input type="submit" id="postProposal"/>
Would call this action:
[HttpPost]
public JsonResult Save(string name, string description, string imgID)
{
return Json("a");
}
(This is the current implementation, it has no logic because I am still testing some things).
My problem is: when I click on my submit button, the action is called with the correct values, but when it returns, my browser redirects to /Upload/Save (which is the action URL).
Why is this happening? Is there any way to prevent it?
Thanks!
You can use an asynchronous call to your method (e.g. AJAX) to prevent the page from reloading.
You can use Ajax.BeginForm to prevent reload page.
As I can see you use full page reload with:
action="/Upload/Save"
Also you trying to post data to json action method in controller. You need all form submit make with jquery, most problems you will find with ajax file uploaders.
Added:
Also look at serialize() it will help you to collect all form input values.
Dont need to prevent it, just redirect the user in your server code .

Add a search box to a master page

I'm trying to add a search box to a master page in an ASP.Net MVC web app. What's confusing me is how to properly implement it in the master page. So the user types in data into this search box, how should the data be handled from an MVC perspective?? I know I could use he master page's code behind, but I shouldn't. I'm currently trying to use a user control for this, but I'm not sure how to properly implement it and online resources seem to be limited. Would creating an HTML helper be best??
To summarize: Implement a search box in the MVC master page that directs to a different website and includes the user's query that they typed in the search box.
Is it better to use:
Master Page's codebehind
A user control
Or create a separate HTML Helper.
UPDATE:
Ok, per queen3's advice, I implemented a SearchController and used the HTML Helper BeginForm to generate a search box.
Controller action:
Function SearchWiki(ByVal q As String) As ActionResult
Return Redirect("http://home/search/Results.aspx?k=" & q & "&s=IT%20FAQ")
End Function
And in the Master Page:
<% Using Html.BeginForm("SearchWiki", "Search", FormMethod.Post)%>
<input type="text" name="q" />
<input type="submit" value="Search" />
<% End Using%>
But when I try to debug, the SearchWiki function never gets called and, as a result, nothing happens when I type in the search box and hit Search.
Forget about codebehind and user controls if you're going to use ASP.NET MVC. You need HTML, CSS, and JavaScript.
I suppose you want something like
<form action="<%= Url.Action("Index", "Search") %>" method="post">
<input type="text" name="q" />
</form>
With helpers it will be something like
<% Html.BeginForm("Index", "Search") %>
<input type="text" name="q" />
<% Html.EndForm() %>
Just put this into master page where appropriate in you site design. Then create SearchController to handle request, and return View() with search results. You may make form use GET instead of POST if you accept google-like search requests /Search?q=text.
The controller is very simple:
public class SearchController: Controller
{
public ActionResult Index(string q)
{
return View(SearchHelper.DoSearch(q));
// or return Redirect("http://site?q=" + q) if you want redirect
}
}
To summarize: Implement a search box
in the MVC master page that directs to
a different website and includes the
user's query that they typed in the
search box.
Seems like you want to use a different search provider. In this case you don't need any server-sided code at all... only pure html. I'll give you an example with Google:
<form id="search" action="http://www.google.com.br/search" method="GET">
<input type="text" name="q" />
<input type="submit" value="Submit" />
</form>
Just add this code on your MasterPage and we're done.
You can also add some JQuery to append the string "site:www.yoursite.com" to the search query. Doing so you can ask google to search the keywords on your site. The javascript code should be:
$("#search").submit(function(){
var input = $(this).find('input[name=q]');
var query = input.val() + ' site:www.yoursite.com';
input.val(query);
});

asp.net mvc post to different views in same form

I have form area in my view. If I click button A, I want to submit to /Books/1 and if I click button B, I want to submit to /Books/2
How do I achieve this with MVC?
<form id="form1" name="form1" action="/Books/" method="get">
<input type="text" name="search" value="">
<input type="submit" name="id" value="1">
<input type="submit" name="id" value="2">
</form>
It sounds like what you want to do is call the Books Controller, with, say, the Search action. So for instance you might want to call /Books/Search/<search expression>/1, or /Books/Search/<search expression>/2, etc. (There's a few different ways you could be formatting these URLs, but it's mostly a matter of personal preference I think) If you want the URLs to appear as you've got them above (without the action in the URL), that can be accomplished with routing, something like this:
routes.MapRoute(
"Books",
"Books/{searchExpr}/{pageId}",
new { controller = "Books", action = "Search", searchExpr = "", pageId = 1 }
);
I think the main problem is that you're trying to use the WebForms PostBack everything paradigm in a situation where you're probably better off sending the information to the server in the URL or query string. The only time you're actually going to be posting form data here is when the user actually types something into the search box and clicks the Search button - at that point, the controller will pass the search expression to the appropriate View by stuffing it in ViewData, and from there, the View can pull it out and repopulate that textbox on the results page.
MVC Views can have multiple forms on a 'page', so just create separate sections and give each one their own form action.
<form id="form1" name="form1" action="/Books/1" method="get">
<!--...form fields-->
</form>
<form id="form2" name="form2" action="/Books/2" method="get">
<!--...form fields-->
</form>
I have never seen the ability to have a form field attached to two forms, seems like it wouldn't work. What you can do is put a hidden field in the second form which, on submission, grabs the information from the textbox in the first form.

Resources