Handling routes in asp.net mvc - asp.net-mvc

I have an action that I want to use to handle the saving of a report for a project and a sub project. The code to run is essentially the same so I thought I could create a custom save action and then specify the routes; however, it doesn't seem to be working as expected.
My action signature is
[Route("Project/{projectNumber}/Reports/ProjectHoursAndCosts/save")]
[Route("Project/{projectNumber}/Reports/ProjectHoursAndCosts/{subProjectNumber}/save")]
public ActionResult ProjectHoursAndCosts(ProjectHoursAndCostsViewModel projectHoursAndCostsViewModel)
If I remove one of the routes it works as expected. Any ideas?
I've set the action in form
(Html.BeginForm("Save", "Reports"))
If my initial url is
http://somehost/somewebapp/Project/61033651/Reports/ProjectHoursAndCosts
it seems to work ok, however if I try to save a sub project which has the url
http://somehost/somewebapp/Project/61033651/Reports/ProjectHoursAndCosts/61033651-1
When I hit save the url changes back to
http://somehost/somewebapp/Project/61033651/Reports/ProjectHoursAndCosts/save
I would expect it to submit to the url
http://somehost/somewebapp/Project/61033651/Reports/ProjectHoursAndCosts/61033651-1/save
Update
Looking at the raw form element in the html page I can see that even though the url is http://somehost/somewebapp/Project/61033651/Reports/ProjectHoursAndCosts/61033651-1, the actual post url is still somewebapp/Project/61033651/Reports/ProjectHoursAndCosts/save
How can I change the action attribute of the form to use the correct url?

Related

how to use resolve url in mvc controller

Can any body help me to resolve url (route url) in MVC
when i enter into the system it redirects me to Dashboard it works perfectly.
I have user defined menu whose routing value stored in database as follows
suppose i entered into the system it will redirect me to Dashboard page
suppose i want to redirect to usermaster page then url should be as follows
http://localhost:6782/Home/Index
but when i try to redirect to home index page the url should look like this
http://localhost:6782/Dashboard/~/Voucher/Create
i want to remove Dashboard/~
how plz let me know
If you're using IIS URL Rewriting within your MVC application, e.g. internally treating http://yoursubdomain.example.com/MyController/MyAction as http://hosted.example.com/yoursubdomain/MyController/MyAction, Url.Content() will generate a correct subdomain-relative link. ResolveUrl() will generate an incorrect link in this situation.
Source:Should I use Url.Content() or ResolveUrl() in my MVC views?
You should consider about #Url.Content("~\Action\").
You should not store the URL in the database as the URL may change according to the routing rules.
Try to store the controller name and action name in the database and in your code that builds the menu use #Url.Action and pass the controller and action retrieved from the database

return url is not working on Live server vs development server

I have an MVC3 Razor view which has a link that requires user login and its html is like this:
<a href="Home/Login?returnUrl=/myprojname/Product/Index">
and then an action method Login in homecontroller and a view. Which posts values to Logon action method.
inside the post action method I check if returnurl is not null then redirect to that URL. On local system it returns to this address
http://localhost:1235/myProjname/Product/Index
and works fine. But when I upload it to server (IIS 7), It returns to
http://domainname/myProjname/Product/Index
and gives 404 because there is no 'myProjname' there, If I remove the 'myprojname', it works with this url on live server
http://domainname/Product/Index
but for that I need to change href like this
<a href="Home/Login?returnUrl=/Product/Index">
In that case, It doesn't work with local system with URL
http://localhost:1235/Product/Index
Please suggest solution
Are you serious?
Okay, we'll i'll play along.
The solution is to create links properly, not hard code them.
#Html.ActionLink("Login", "Home", new { returnUrl = Url.Action("Index", "Product") })
On a side note, you probably don't need to pass returnUrl to the action. Pull it back from the Request.UrlReferrer.AbsoluteUri in the action method itself.
Not meaning to sound rude, but I strongly suggest you head over to http://www.asp.net/mvc and have a read/watch before proceeding any further.
using Url.Action and Url.Content helper methods will generate URLs relative to the application root, and avoid these problems.
Also, #Href("~/") will generate the full path to the application root (from a view).

Asp.Net MVC Form[GET]. Correct action url

I've got search form, which contains checkboxlist which is binded to my model. So when I set GET method to form I got long url:
(I even have exception:
The length of the query string for this request exceeds the configured maxQueryStringLength value.)
it's expecting, [0].IsSelected=false&[0].Id=6&[1].IsSelected=false...
But I would like url like this
www.domain.com/Action/Comma-separated-selected-idx
for example:
www.domain.com/Search/1,6,7
How can I fix,edit form get action? Thanks
I would do a POST instead, then redirect to the URL you desire.
OR
You could capture the form with some JavaScript and build the URL there.
agreed, POST or JS are your best options, I'd opt for a POST.

Symfony/Routing: Using POST to Avoid Query Params in URL

I want to pass the id from one action to the next action, but I do not want it seen in the URL. Is there a way to hide it?
Using Symfony, I have created a sign-up done page whose URL should be /signup/done
When the form is processed, I want to redirect to the signupSuccess action and pass the recently created ID for future use. So I use...
$this->redirect('#signup_done?id=' . $sign_up->getId());
The routing is as follows:
signup_done:
url: /signup/done
param: { module: SignUp, action: signupDone }
I have avoided the :id at the end because I don't want it in the URL.
But the resulting URL is /signup/done?id=1
Just as an experiment, I tried putting this on a template.
<?php echo link_to('Sign-up again', '#signup_done?id=1', 'post=true') ?>
Even when using post, the query parameter appears in the URL.
The need is: I want to pass the id from one action to the next action, but I do not want it seen in the URL. Is there a way to hide it?
I set the id as a parameter in the request using $request->setParameter('id', $id) and it was available in the next action.
This kept the URL clean.
If you want to post, you need a form. Back in symfony 1.2 there were helpers that you could call and made you just that - but they were removed for the promotion of promoting good code.
Depending on how you want the 'Sign up again' text to look, you can either create a simple form and a submit button, or create a link, attach a click listener, and create a form there via JS, finally post it.
Any parameter that you pass to the route in url_for, link_to and such end up in the get parameters.

Html.Action link and Html.RouteLink

Even If the url is same can i go to different action using Html.RouteLink and Action Link.Like when i click on news link i will go to news details.The url of this page is http://localhost:1390/en-US/latestnews/125.Now if i select the ddl of language in the site header in this pagei need to
go to the home page of the site.The ddl (on change) will take the same url but this time it needs to go to action in the sane controller.
The URL will direct to the controller/action on the first routing entry that it matches regardless of how you generate it. I'm not sure exactly what you are trying to accomplish, but I suspect that what you need to do is use javascript to direct to a different url, perhaps generated with Html.RouteLink instead of Html.ActionLink based on the value of the drop down list when it's selected. If I'm misunderstanding what you are trying to do, please clarify.

Resources