Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
759 views
in Technique[技术] by (71.8m points)

asp.net mvc - Html.BeginForm() with an absolute URL?

I need to have the POST action be to an absolute URL (e.g., http://www.cnn.com). Is there a way to use Html.BeginForm() helper and pass it the url?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

BeginForm method has several overloads. In order to set the action attribute on the form tag with the desired url, you need to use following overload of BeginForm:

BeginForm(String, String, FormMethod, IDictionary<String, Object>)
// here are the parameter names:
BeginForm(actionName, controllerName, method, htmlAttributes)

Since you want to post to an external site, there is no need to set actionName and controllerName, just leave them as null.

@Html.BeginForm(
   null, null, FormMethod.Post, new {@action="http://cnn.com/post"}
)

This will not encode the action parameter.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...