ASP.Net mvc2 url format issue - asp.net

In my mvc2 project, some URL are shown in the browser like this,
localhost:53289/Paper/ViewAgendaPaper?MeetingId=186&type=2&RefId=186
but i prefer to look it like this,
localhost:53289/Paper/ViewAgendaPaper
In my Global.asax,
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Default", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
How to hide the id fields in the URL. Please give me some idea. Thankyou.

As far as I think you can't hide the Ids as they are supposed to be passed to fetch the exact data, however instead of
localhost:53289/Paper/ViewAgendaPaper?MeetingId=186&type=2&RefId=186
you can have it like:
localhost:53289/Paper/ViewAgendaPaper/186/2/186
Going through this post will help you more:
How can I create a friendly URL in ASP.NET MVC?

You cannot do what you are asking for with a simple link. An anchor link <a>...</a> performs a simple GET. If you want to hide the parameters, you would need to do a POST to the page in question.

Related

How to redirect URL from old WebForms site to subdomain on new MVC5 site using MVC routing

We are in the middle of migrating and updating our current website from WebForms to MVC5. One part of the current site will be decoupled and remain running on WebForms as a standalone site for the time being and will become accessible on its own subdomain in the new situation.
In the current situation we have a bunch of URL's looking like this:
www.site.com/foo/[..]/bar.aspx
In the new situation we would like to redirect these URL's to something like this:
www2.site.com/foo/[..]/bar.aspx
I was thinking of using a Route in MVC5 that captures all incoming requests starting with foo and routing it to a Redirect controller. Something like this:
routes.MapRoute(name: "RedirectFooBar", url: "foo(/.*)", defaults: new { controller = "Redirect", action = "FooBar" });
public ActionResult FooBar()
{
return RedirectPermanent(Request.RawUrl.Replace("//www.", "//www2."));
}
Unfortunately, my route is not working like this, because of the regular expression in the url. I would like to know if it is possible to achieve what I'm trying to do with MVC routing and if it is possible, what changes should I make to get this to work?
Or maybe there are better ways to achieve this sort of thing?
Thanks!
Your idea is correct, but you're using the wrong syntax. You should define your route like this:
routes.MapRoute(name: "RedirectFooBar", url: "foo/{*path}", defaults: new { controller = "Redirect", action = "FooBar" });
The {*path} will capture everything from this point to the end of the url. You can then use it as a parameter in your action:
public ActionResult FooBar(string path)
{
return RedirectPermanent("http://www2.site.com/foo/" + path);
}

Html.Beginform forcing addition of controller name

In MVC4, using
Html.BeginForm("/", null, FormMethod.Post, new{something="demo"})
the resulting form has
action="//"
Because it is apparently trying to squeeze the controller and action into the url. However, I'm using the HomeController as the default controller, so I don't want /Home/action in the url. This has been working fine in most places, but this form seems to be tripping me up
Any ideas?
That's not the controller name. You would do something like this:
Html.BeginForm("Index", "Home", FormMethod.Post, new {something="demo"})
If your routes are setup correctly and Home/Index is your default root route, then when rendered, it will render to "/". MVC will automatically optimize the route to show the default minimum (which is the lone "/")
If your not seeing "/" then look at your routes, as it's not able to reverse generate the URL correctly (probably more than one route matches).
If you are using Home/SomethingElse and it's not the default route, then I don't quite get what you're trying to do, since you would be required to specify the Controller/Action to post to it.
While I don't recommend it, you should also be able to do this:
Html.BeginForm(null, null, FormMethod.Post, new {action="/", something="demo"})
I would consider this a hack, however, and I would encourage you to fix the actual route problem.

RouteTable.Routes.MapPageRoute with optional parameter

I'm in an ASP.Net 4.0 web forms app.
I have this routing already in place in Global.asax and working:
RouteTable.Routes.MapPageRoute("status", "members/{userid}/{status}", "~/members/status.aspx");
The 'status' parameter will let me set active tab on the page to either status, blog, photos or about (default is status).
So when I post to: /members/status.aspx?userId=first.last
I'll see /members/first.last/status when the page loads.
What I need to do is render some links that will take user to the blog tab with a blog id so I can land on blog tab, which has multiple blogs displayed, and scroll to specific blog by id. I'm trying to set the href similar to how we do it in MVC: /members/first.last/blog/1000 (where 1000 is blog Id) - my other attempt is /members/first.last/blog/?id=1000 but I can never see the Id in the querystring keys in Page_Load.
I've tried adding this routing to no avail:
RouteTable.Routes.MapPageRoute("status",
"members/{userid}/{status}/{*queryvalues}", "~/members/status.aspx",
false,
new RouteValueDictionary { { "id", #"\d{4}" } });
Thanks for any assistance.
I think you should look in the items collection.
this.Context.Items["userid"]
or in RoutData collection for 4.0
Page.RouteData.Values["userid"]
see this link for info How to: Access URL Parameters in a Routed Page
Use as below:
routes.MapPageRoute(
"ProductsBrowse",
"browse/{BrowseBy}/{Category}/{*queryvalues}",
"~/Pages/Products/Browse.aspx"
);
Thx: asp.net webforms routing: optional parameters

MVC4 link automatically redirected to default INDEX page/action even if defined action name with controller

i am creating web mobile application in mvc4.
My problem is when I click on particular link in my application,it works well,
but sometimes it automatically redirected to INDEX page that is set as default page in global.asax as
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Now I don't know why its automatically redirected to INDEX page,even if I have already defined controller and action name where it show redirected as,
<a href='#(Url.Action( "ActivityWall", "Home"))' > </a>
logically it should redirect to "ActivityWall" page,which it does.but sometime only it goes to INDEX page.then when I clear my cookie problem will again solved but after some time it again start redirecting to INDEX page.
I also posted question related to cookies issue yesterday,but I think that is nit main issue.
can someone help please ?
You'll need to catch this in action with Fiddler or firebug, etc and watch if a redirect really comes from the app or if something else happens. Your link above should stay going to /Home/ActivityWall, but without seeing your code inside ActivityWall we can't say for sure whats going on there.
Watch the location every tine you click on that link. If a redirect comes back either you have an action filter doing a redirect or your code does it for some reason you aren't aware, or its a security misconfiguration.

ASP.Net MVC Controller and View interaction

I created a sample MVC application using following Template.
ASP.NET MVC2 Empty Web Application
Then a added a Controller with the name of First and a right clicked the ActionResult to add a View.
I typed http://localhost:49565/First in my Browser.
Query
How is the controller internally getting to know that a specific page will be displayed when we will type http://localhost:49565/First ?
Moreover, If I add multiple Views for a Controller. How will the system decide which one will be displayed on Priority ?
The controller is invoked by the MVC framework, which uses the routes defined in Global.asax.cs to determine which controller and action to invoke. There is a default route that looks like this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
When the application receives a request is will try to parse the URL to the format of the routes. If the request is made to http://localhost:49565/, it will use the default values which goes to the Index action in the controller named HomeController. When you have created the new controller, FirstController, and call http://localhost:49565/First, it uses the FirstController instead of the HomeController since it has been provided (but still to the Index action).
Further, when an action is being invoked and there is no view defined explicitly, it will look for a view named the same as the invoked action. In your case it would be ~/Views/First/Index.aspx.
EDIT
If you want to use another view you can specify it in the return statement
return View("OtherView");
and it will use ~/Views/First/OtherView.aspx instead.
Have a look at this blog posts give u the idea of how it is done

Resources