Is there a way to generate the post URL in the GET action if both the GET and the POST methods share the same name?

Is there a way to generate the post URL in the GET action if both the GET and the POST methods share the same name?

The reason for needing to generate the post URL in the controller is that we aren't dealing with the views but only providing the ViewModel that contains a payload in the form of JSON. And the front-end needs to know where to post form values to.

[HttpGet] public ActionResult MethodName(string id) { ... } [HttpPost] public JsonResult MethodName(ViewModel model) { ... }

e.g. the get URL would be:

/controller/methodname/123

and the post URL would be:

/controller/methodname

in the get action, I need to feed the front-end with the post URL of which the form values would be consumed.

using Url.Action("methodname", "controller", new { id = string.Empty}) would generate a URL that's the same to the post URL, but it's sort of hack-ish way

Another option would be to define a route for the post method and use Url.RouteUrl("PostMethodName") to generate the URL.

Neither of these seem ideal, so is there another (cleaner) way to get this done?

--------------Solutions-------------

Walter,

I am failing to understand how this is "hack-ish." You are providing a restricted post action with a strongly typed model, the only difference if your action result type. The URL's can be the same if you wish.

Alternativtly you can create just an action for your aJax method such as.

[HttpPost]
public JsonResult ajaxMethod(ViewModel model){
//...
}

This is restrictive to just the HttpPost method and contains your view model. Actions dont need views they simply return an action result. If you wanted to get very bare-bones and action can be an object returning anything. Your route for this action is similar to all other routes:

@Url.Action("ajaxMethod", "controller")

to generate

/controller/ajaxMethod

I may have completly missed the question, please let me know if this doesnt help.

EDIT: Updated from comments.

Just to add if you would like to generate a route or action url in your controller action you can simply use the Url helper in your action such as.

ViewBag.Url = Url.Action("MethodName", "Controller");

Another option is to create a custom Route implementation. A very basic one that includes a line to fix your problem (Drop the existing route parameters) can look like this:

public class CustomRoute : Route
{
public CustomRoute(string url, IRouteHandler routeHandler)
: base(url, routeHandler)
{
}

public override RouteData GetRouteData(HttpContextBase httpContext)
{
var routeData = base.GetRouteData(httpContext);
if (routeData == null)
{
return null;
}

/* custom implementation here */

return routeData;
}

public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
/*
* GetVirtualPath is called until a suitable route is found.
* This means modifying the passed RouteValueDictionary has consequenses for subsequent calls.
* Therefore, always work with a copy if you want to modify any values: new RouteValueDictionary(values)
*/

/*
* Drop the existing route parameters. Implicitness can only cause confusion.
*/
requestContext = new RequestContext(requestContext.HttpContext, new RouteData());

return base.GetVirtualPath(requestContext, values);
}
}

You probably also want to have MapRoute methods for your custom route. For this, you just need to look at the original implementation, copy paste it and replace Route with CustomRoute.

Category:asp.net mvc Time:2014-01-07 Views:1
Tags: asp.net mvc

Related post

  • How do I generate a Friendly URL in C#? 2008-09-01

    How can I go about generating a Friendly URL in C#? Currently I simple replace spaces with an underscore, but how would I go about generating URL's like Stack Overflow? For example how can I convert: How do I generate a Friendly URL in C#? Into how-d

  • Is it safe to use Python UUID module generated values in URL's of a webpage? 2009-02-19

    Is it safe to use Python UUID module generated values in URL's of a webpage? Wnat to use those ID's as part of URL's. Are there any non-safe characters ever generated by Python UUID that shouldn't be in URL's? --------------Solutions------------- The

  • Generating User-Friendly URLs 2009-07-02

    Is there a standard way / C# library to convert a string into a user friendly uri in order to use it in a RESTful MVC environment? StackOverflow does this, for example "èòà" are converted to "eoa", question marks are removed while dots, commas and br

  • php - generate non linear urls 2009-12-06

    Update: Ok.. I have read a lot of solutions... Thank you so much everybody. I think I will keep it simple and avoid the encryption and just rely on two fields matching in the database. I can keep the id as it is (sequential) but add for example a tim

  • Rails: generate a full URL in an ActionMailer view 2010-02-13

    I'm using ActionMailer to send a sign up confirmation email. The email needs to contain a link back to the site to verify the user, but I can't persuade Rails to generate a full URL (including the domain etc). I'm using: <%= url_for :controller =

  • How to generate just the URL of the Controller method in ASP.NET MVC 2010-03-21

    So Html.ActionLink("Report", "SendMail", "Misc", new { id = Model.ImageID }, null) will generate a nicely formatted link. <a href="http://localhost:3224/Misc/SendMail/5">Send Mail</a> How can I generate just the URL of the link, short of

  • MVC ActionLink generating NON-Restul URL AFTER adding constraints 2010-04-16

    I have a custom route without constraints that generates a Restful URL with an ActionLink. Route - routes.MapRoute( "Blog", // Route name "Blog/{d}/{m}/{y}", // URL with parameters, new { controller = "Blog", action = "Retrieve" } Generates - http://

  • What is the most mature way/lib to generate SEO friendly URLs (slugs)? 2010-06-20

    I am looking for the most mature library, algorithm or code snippet that can generate SEO friendly and meaningful URLs from titles (e.g. www.example.org/what-is-the-most-mature-way-to-generate-seo-friendly-urls-slugs). I know one can easily write a 1

  • How can I generate an absolute url for a Pylons controller? 2010-10-13

    I need to generate a URL for use as a callback in an external system in my pylons application. This requires me to provide both the pylons-app-relative controller path (as generated by the url method: >>> relative_url = url(controller='my_co

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.394 (s). 13 q(s)