i want to make some questions about asp.net mvc.Actually,i am not familiar with web developing.my questions are i have two controllers,one is login and another one is
profile.After login,i want to call profile.so,i use this way,
return RedirectToAction("DiaplayProfile","Profile",new { personID = Person.personID});
my problem is parameter that i passed are shown i URL.i don't want to show.In web developing,if we use post method to send data,parameters are not shown in url,is it correct?for me,i need to pass parameter ,but,i don't want to show at url.
How i use in post method in MVC RedirectToAction?i also tested with TempData
to pass data,but,it's gone after if i make refresh.how i hanlde this?And also,
can i use [AcceptVerbs(HttpVerbs.Post)] for solve this problem?
guide me right ways, please.
Regards
Chong
You can't do a post as part of a redirect; a redirect must be a get request since you're simply passing the browser the URL to which to redirect. In MVC, having the id as part of the URL is actually considered to be a good thing. I would embrace it. Rather than having the parameter be personID, I would use id so that the default routing can be used. Eventually I'd want to end up with a URL that looks like:
http://example.com/profile/display/1345
where 1345 is the id of the person in question. If you use default routing, that would mean that you have a ProfileController with a Display method that takes a single id parameter (of type int). You would redirect to it using:
return RedirectToAction( "display", "profile", new { id = 1345 } );
BTW, you can't use the AcceptVerbs attribute to control how a redirect is made, only to control what HTTP verbs the action will respond to. In this case, I think you really do want to accept a GET and I think you really do want to have the id in the URL.
Related
Consider StackOverflow, where each question has a unique ID, but URLs are often overridden to include a stub in the URL. For readability and other reasons the stub helps users know they are at the right place.
I have a site that returns 200 when calling a URL like:
http://stackoverflow.com/questions/28057406/
But want the URL to update to:
http://stackoverflow.com/questions/28057406/is-it-possible-to-return-http-code-200-but-give-a-better-url-without-using-3x
The first call is technically valid and the code can retrieve the object and render it perfectly fine, but I'd like to update the URL to use the stubified one.
I'd prefer to do this without a redirect as just getting the ID causes a database call to get the object. Which would mean with a redirect the process would be:
Call http://stackoverflow.com/questions/28057406/
Retrieve item 25257999 from the database to get the name to make the stub
Redirect to http://stackoverflow.com/questions/28057406/is-it-possible-to-return-http-code-200-but-give-a-better-url-without-using-3x
New HTTP Call, so retrieve item 25257999 from the database to render the final page.
If possible I'd like to not use Javascript either.
So, is it possible to return Location as part of a HTTP header with a status code of 200 and the actual page, or am I stuck using 3xx calls or Javascript?
If you are just doing HTTP, you can either choose to redirect, or not choose to redirect... You can also (with Content-Location) tell the client that the canonical address is actually somewhere else... but no browser will respond to that.
To avoid the database-call, you could of course just cache the result.
If you are in a browser however, you can dynamically update the current address without forcing a refresh, with window.history.pushState.
For more information about that call, see this other SO answer:
Modify the URL without reloading the page
it seem that we need to specify a route for every page in webform routing
I want to use default route to a page called cms.aspx with parametr called nameofurl for each page expect default.aspx
sometimes I want to send the cms.aspx more then one parametrs,for example
mydomain.com/cms.apx?nameurl=somevalue
or
mydomain.com/cms.apx?nameurl=somevalue&order=6
I have this code but it isn't the solution since you have to tell the routing the name of the page
routes.MapPageRoute("",
"pageName/{nameofurl}",
"~/cms.aspx")
I want something like this
routes.MapPageRoute("",
"?/{nameofurl }",
"~/cms.aspx")
sometimes I want it to be like this
routes.MapPageRoute("SalesRoute",
"?/{nameofurl}/{order}",
"~/cms.aspx");
any idea how to Achieve that kind of routing without specify the name of the page?
You can create routes like given below:
routes.MapPageRoute("Route1","{nameofurl}","~/cms.aspx")
routes.MapPageRoute("Route2","{nameofurl}/{order}","~/cms.aspx")
routes.MapPageRoute("Route3","{nameofurl}/{order}/{abc}","~/cms.aspx")
The above routes will work if there are no other pages with 2 or 3 parameters. But if there is some other page which you want to route and which has 2 parameters to be passed, then you need to mention a hard-coded string before the parameters otherwise the new route will override the old route.
For Example:
routes.MapPageRoute("Route4","{nameofurl}/{order}","~/products.aspx")
In the above case, Route4 will override Route 2. Thus, you need to define route something like below:
routes.MapPageRoute("Route4","products/{nameofurl}/{order}","~/products.aspx")
You can find URL Routing related articles at following URLs:
http://karmic-development.blogspot.in/2013/10/url-routing-in-aspnet-web-forms-part-2.html
Thanks & Regards,
Munjal
I have a page "Demo.aspx". I need to set some parameters using post method and redirect the page to "DemoTest.aspx".
Is there any way to set parameters in post method in asp.net? I don't want to set "Querystring" due to security propose.
Also I need server side code for this. I am not able to use "Javascript" or "Jquery" for the same.
Here is some more description for the same.
Right now I am using Response.Redirect("www.ABC.Com/DemoTest.aspx?P1=2"). So the page is simply redirect to the given URL.
Now I don't want to pass that "P1" in "Querystring". Instead of query string I want to use Post method.
Please note that redirection page is not in my own application. So I cant maintain session or "Viewstate".
Thanks in advance.
Use a session variable and response.redirect to the next page.
Session["MyVariable"] = "someThing";
Response.Redirect("DemoTest.aspx");
The value stored in Session variables will be accessible across application.
you can store in session like this :
Session["id"] = "anyID";
To get values On another page you need to write
string id = Convert.ToString(Session["Id"]);
However
By default, in .NET pages post() do the things automatically.
You will need to do sumthing like this:
Server.Transfer("DemoTest.aspx", True)
I have googled to death this to no avail. I have a page that we no longer want to use, however, dpending on how it was called, I want to pass the querystring along if there is one. I wrote some ugly code to add the querystring if there is one, but if the Response.RedirectPermanent already does it then I could skip that step. The microsoft web site is not clear. Thanks.
A 301 redirect will redirect to whatever you pass into RedirectPermanent, exactly that. It will not re-append the curent query string to what you are redirecting to.
I would like to POST an entity as follows
POST /example.org/MyEntity/100
Based on the passed entity, the server would like to draw the users attention to a particular part of the response using a fragment identifier. e.g.
/example.org/MyEntity/100#InterestingPart
How do I return this new URL to the client. I am assuming I could do some form of redirect using a 3XX response code, but I actually do not want the client to do another request because the only difference between the two URLs is the fragment. At the moment it seems that a 307 return code would be the most appropriate because according to the spec you should not automatically redirect a POST.
Is there are better way?
Update: My client is not limited to the constraints of a web browser. I am just looking at this from the perspective of HTTP.
Update2: Based on my reading of RFC2616, I can see nothing stopping me from returning a 200 and a Location header that contains the fragment identifier. Anyone know of a reason why I cannot do that?
I think the only sensible solution is to have action URL have static fragment identifier, like <form method="post" action="/action#anchored"> and then put an anchor wherever you want user to look at while generating page.
But, to answer the Update2: no, there's no reason to avoid it.
My inclination is to return 201 - and have the location header point to the URI you want the client to GET.
I didn't look, but IIRC nothing dictates that the location header points to the resource created, so it should be spec legal.
You should normally redirect every POST to avoid problems with refreshing the page and the use of the back button. This is known as the PRG (POST Redirect Get) pattern:
http://blog.httpwatch.com/2007/10/03/60-of-web-users-can%E2%80%99t-be-wrong-%E2%80%93-don%E2%80%99t-break-the-back-button/
Although, this does incur the cost of another round trip to the server it makes your web application much more user friendly.
You could then add the fragment onto the redirected URL.
There's an example of PRG with a fragment on this page:
http://www.httpwatch.com/httpgallery/redirection/
POSTing to the URI:
http://example.org/MyEntity/100
implies to me that a MyEntity resource called "100" already exists. If that's the case, why not use PUT instead? Is this an update or a create operation?
An alternative might be:
POST http://example.org/MyEntities
Now your service has a choice to make from at least two possibilities:
Return 201 Created. Set the Location header to be the URI you want the client to use (e.g.: http://example.org/MyEntities/100#InterestingPart). Add the representation of the new resource to the body.
Return 204 No Content. Same as above, but no body. This option requires a subsequent GET to fetch the representation, which sounds like what you're trying to avoid.
Neither approach requires redirection and both can return as specific a URI as you desire.
I am curious though, why is the #InterestingPart significant? Why not just return the entire representation and its URI http://example.org/MyEntities/100 in the Location header - and let clients decide for themselves what's interesting or not? If the answers have something to do with only a small part of the resource being of interest (or being modified) during a request, how feasible would it be to break MyResource into a main resource and one or more subordinate resources? For example:
/MyResources/100/CoolThings
/MyResources/100/CoolThings/42
/MyResources/100/InterestingThings
/MyResources/100/InterestingThings/109