I am new to Asp.net MVC.
I am creating web application, where i have to rewrite url with product name.
I am not sure if that is possible or not in MVC.
Like,
http://sitename.com/category1/product1
http://sitename.com/category1/product2
will have same page.
There are facilities to generate friendly urls within MVC.
Check out the article at - http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/asp-net-mvc-routing-overview-cs - for an overview of how this is handled in MVC.
Essentially, you need to configure the routes on application startup as follows. This can usually be done in the global.asax file but cna be split for areas etc.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes); // Reigster the routes
}
This is the default route but you can define as required.
Related
I am converting an asp.net webforms website type project to an asp.net mvc web application. I want to keep all the changes to a minimum.
The pages and its subfolders are right at the top of the project folder and there are hundreds of them.
Now, in the new web application, I want to move them to a subfolder, let's call it WebForms.
Is there a way at runtime to run the pages as they ran before, i.e. at the root of the application folder?
Before I had: http://localhost:54321/Page1.aspx. Page1.aspx was stored in the website project root folder.
In the new project structure I have the disk:
<project folder>
WebForms
Page1.aspx
This works: http://localhost:54321/WebForms/Page1.aspx, but I want to somehow map it to http://localhost:54321/Page1.aspx.
Is it doable? I use IIS Express for development and IIS 7.5 for test/production deployment. I want to avoid having to change the image and other content urls - as you can imagine moving the pages to the subfolder breaks some of them.
Thanks
If you just want to map all requests to /something.aspx so they go instead to WebForms\something.aspx, you can probably just use the following route rule.
routes.MapPageRoute(
"Other Web Pages",
"{pagename}.aspx",
"~/WebForms/{pagename}.aspx");
Alternately, if you need more advanced scenarios, you could use a custom class that derives from RouteBase and use a RegEx to match for and map the route, similar to this question's answer
public class WebFormsRoute : RouteBase
{
Regex re = new Regex(#"^/(?<page>\w+)\.aspx", RegexOptions.IgnoreCase);
public override RouteData GetRouteData(HttpContextBase httpContext)
{
var data = new RouteData();
var url = httpContext.Request.FilePath;
if (!re.IsMatch(url))
{
return null;
}
var m = re.Match(url);
data.RouteHandler = new PageRouteHandler("~/WebForms/" + m.Groups["page"].Value + ".aspx");
return data;
}
}
And then add it to your route collection in RouteConfig
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(new WebFormsRoute());
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
I am new to azure, MVC and also ASP.NET. I am writing MVC Cloud service with ASP.NET web role. Please help me with this problem
When I create the application there are default views but I wanted to see my view so I set my view as start page. I also changed the values in RegisterRoutes method
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "User", action = "AddUser", id = UrlParameter.Optional }
);
}
When I run the app, it gives HTTP 404 error because it could not find request URL : /Views/User/AddUser.cshtml
In MVC you don't put the view in the URL to get it rendered.
This won't work: /Views/User/AddUser.cshtml
As you've correctly put in your question the default route is {controller}/{action}/{id} with id being optional.
So assuming that User is your controller, i.e. you have a class called UserController, which looks something like:
namespace My.Controllers
{
public class UserController : Controller
{
which has an action on it called AddUser:
public ActionResult AddUser()
{
// implementation logic
return View();
}
Then the default route will display your view when it processes the URL /User/AddUser
In MVC 5, this looks something like:
i have inherited an application that is both clasic asp.net web forms and mvc (started as web forms project). I would like to setup routing like MVC. What is the best way to go here? All i need is a push in the right direction.
I know how to setup routing for the MVC only project via global.asa > App_Start > Route Config and area registration cs files.
Environment:
VS 2012, IIS 7, ASP.NET 4.0, classic asp.net web forms and MVC 4.
My thinking:
I am thinking about doing some thing like following, do you guys see an issue here? I may end up with some web.config issues but at this this time i am not sure what those would be. I need your advice to properly setup the structure here.
Global file addition:
protected void Application_Start()
{
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
App_Start > RouteConfig.cs
namespace My.Site
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "My.Site.Controllers" }
);
OTHER ROUTES WILL GO HERE, THESE MAY REDIRECT TO a webform page or to a controller action.
}
}
}
Going through the following to setup all properly.
http://www.hanselman.com/blog/PlugInHybridsASPNETWebFormsAndASPMVCAndASPNETDynamicDataSideBySide.aspx
http://www.hanselman.com/blog/IntegratingASPNETMVC3IntoExistingUpgradedASPNET4WebFormsApplications.aspx
http://www.packtpub.com/article/mixing-aspnet-webforms-and-aspnet-mvc
http://msdn.microsoft.com/en-us/library/dd329551.ASPX
I am developing a website with ASP.net MVC3.
I have built it up and ran normally at local. Then when I deploy it with IIS 7.5, the site can display. All functionality works except the url is not changing when I switch in between actions and controllers(the url always shows "www.mysite.com" not "www.mysite.com/home/action"). Also, the title of pages are not shown. Instead of my slogan, it shows the domain url like www.mysite.com on the page title of browsers.
I followed the official deploy instruction of ASP.net with IIS.
Is there anyone knows what's the problem? Thanks in advance.
Here is my code for the Global.asax
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"ImageWall", // Route name
"ImageWall/{action}/{id}", // URL with parameters
new { controller = "ImageWall", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
}
Regards the title, in your views, do you use something like
ViewBag.Title = "Some Title"
In the HTML source of a page on your site, does the <title> element get populated? If not, then that could explain why you do not see the title.
Regards the url, can you post the code of your global.asax.cs file please (or global.asax.vb if you're using VB.NET). That is where route configuration can take place, so seeing that could help us explain why the url does not a show.
I have found the problem.
The problem is caused by the settings of Domain.com. As I am a fresher to deploy, I didn't set the "A Record" for all my domains. I just set the redirection to my IP.
I am converting a classic ASP.NET 4.0 site to also use MVC. Over time I am migrating the ASP.NET code to MVC, but during the transition both technologies will be in use.
If I navigate to the default page (ie, http://mywebsite.com/), then MVC routing is taking over and returning the following message.
This request has been blocked because sensitive information could be
disclosed to third party web sites when this is used in a GET request.
To allow GET requests, set JsonRequestBehavior to AllowGet
If I use http://mywebsite.com/default.aspx, then everything works fine.
My route config looks like...
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//ignore aspx pages (web forms take care of these)
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Notice I am ignoring .aspx page requests, so these requests get ignored by the MVC pipeline. However, I need 'no page specified' default requests to process default.aspx. How would I change the above code or configure the site/IIS to make this happen?
I removed the default route and now the behavior is as needed.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//ignore aspx pages (web forms take care of these)
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
}