I have a MVC website project to design it , I don't have any knowledge about MVC coding :( . All I want to do that create new .cshtml page in (views/home) folder, which will be welcoming page (or new home page), and display this page before the existing page (home/index) which will open by link or button in my new home page.
Please, I want to knew method.. step by step .
Note: the developer gave me a beta link of site which don't contain a many folders like (Models , Controllers , App_Start , ...).
I hope It's clear,
Thanks in advance. :)
first you can create new Action in Home controller. Right click on this Action and create view for this ActionResult Newpage.
public ActionResult NewPage()
{
return View();
}
change RouteConfig.cs file under App_Start folder like this
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "NewPage", id = UrlParameter.Optional }
);
now build your application and run your project. Do you learn more about MVC CRUD?
You can change the default homepage in App_Start > RouteConfig.cs.
Change its controller and action to your welcoming page.
In your welcoming page, just set up a button that redirects the page to your homepage.
Related
I'm working on MVC5.
I already have my controllers and their respective views. If I click on a view and open it on my browser, everything's fine, however, whenever I start the project normally on VS, my browser opens, for example, this link:
http://localhost:50738/Views/Profile/Index.cshtml
However, whenever I open the view directly I have:
http://localhost:50738/Profile or http://localhost:50738/Profile/Index
At the RouteConfig file I just said that I wanted Profile to be shown by default, instead of home. Why does '/Views/' appear on my browser?
code:
namespace WorkTimeManager.Presentation
{
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 = "Indicators", action = "Index", id = UrlParameter.Optional } );
}
}
}
Its because you have your Start Action set to current page in your web project. So when you start your web app it will try to load the page you are currently viewing in Visual Studio.
To fix this, right click your web project in your solution and click properties, then click on to Web and you will see Start Action - with Current Page radio button selected.
Change this to Specific Page and then type in your homepage URL e.g. http://localhost:50738/Index then everytime you start your web app from Visual Studio it will open on that page instead of trying to open the current page.
When you click on a .cshtml file ("click on a view"), you're only opening the file that describes that view, you're not opening it via 'mvc'.
MVC doesn't send the .cshtml file to the browser, the controller reads the view and renders it.
So the file in Views/Profile/Page.cshtml may be rendered by a controller action ProfileController.Page() (or may not... but would be in the simplest case) for which the URL would be /Profile/Page
This is a brief summary, there are better explanations available of how MVC works.
I have been working on a mvc4 solution and up until now when I pressed the debug button, the browser would open up to the /Home/Index action. no matter what class or view I was working on, it would load the default, just as specified in routeConfig.cs
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 }
);
}
since this morning this has changed. now if I am currently working on /Home/Example and press debug, the browser opens to that instead of Home/Index.
I have no Idea what configuration I changed or what is causing this. any suggestions are welcome.
Your problem has nothing to do with routing. It is related to the default startup page in Visual Studio.
Right click on the ASP.NET MVC project in your solution explorer and choose Properties. Then navigate to the Web tab and on Specific Page write Home/Index:
Maybe in your project you've got Current Page selected.
Did you remove your *.suo files? These files contain information related to the properties of the web application. You can simply right click your web app project in the solution explorer and select the properties menu item. On the properties page select the web tab on the left tab strip and select the radio button labeled "Specific Page" and leave the text box empty. This will ensure your web is always loaded at the root url during debug.
Using asp.net mvc4. I can't hit ~/Account/LogOff from my app. The default route should catch it and route it to the appropriate controller. in routeconfig.cs
routes.MapRoute(
name: "Home",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Shouldn't that catch the url?
Code for the link creation
#Html.ActionLink("Signout", "LogOff", "Account" , new{controller = "Account"
, action = "LogOff"}, new{ target = "_self"})
For some reason that is driving me crazy, I can't hit
//
// POST: /Account/LogOff
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
var fam = FederatedAuthentication.WSFederationAuthenticationModule;
fam.SignOut(false);
SignOutRequestMessage signOutRequest = new SignOutRequestMessage(new Uri(fam.Issuer), fam.Realm);
return new RedirectResult(signOutRequest.WriteQueryString());
}
I've tried adding a route before and after the "Home" maproute. Neither one seems to work. What in the world am I missing here? I've written dozens of asp.net mvc apps and have not had this problem. It's blowing my mind. Please help.
Cheers,
~ck
The reason is because you have a [HttpPost]. What it basically means is that it fetches request coming from a submit button from forms but since your not using a submit button neither a form, your Logoff action will be skipped.
You can resolve this by 2 reasons:
1.) Change the [HttpPost] to [HttpGet]
2.) Just simply direct your link to your action:
#Html.ActionLink("Signout", "LogOff", "Account")
Oh I was doing a get via the link click. The method is for post. I wrapped the link in a form tag and on the link click I submit the form as usual. This fixed the problem and im able to hit my controller method.
You need to remove [HttpPost] attribute from the LogOff() action. #Html.ActionLink() will create a regular link (<a> tag), so clicking on it will issue a GET request.
I new to using ASP.net-mvc and would like some help. I created a web application using VS2010 and need to change the way the pages flow. How do I code it so that the first page the user is presented is not the home page but the login page?
You modify the default route in Global.asax in the RegisterRoutes method.
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Account", action = "LogOn", id = UrlParameter.Optional }
);
You need to change your route (in global.asax.cs) defaults - from controller "home" and action "index", to what you need.
And more importantly you need to read some tutorials and books on technology your are new on, before asking basic questions on SO.
From Stephen Walther's page:
Specific Page – Enables you to set a particular page to run. You can set the page here or you can right-click a page in the Solution Explorer window and select the menu option Set As Start Page.
I have not tried this with mvc 3 app however.
I have an MVC 3 site that is working and currently quite basic. There is a folder off of the root called Blog where I have BlogEngine.net setup. Both work as I want. I had to do the following code in the global.asax file to make sure that MVC would ignore any request going to the Blog folder as follows:
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{folder}/{*pathinfo}", new { folder = "Blog" });
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home",
action = "Index",
id = UrlParameter.Optional } // Parameter defaults
);
}
What I am looking for is to be able to go to: blog.mysite.com and have it go directly to the /blog folder. I have a Subdomain setup through my hosting provider. However, I am not sure what to do beyond this to tell it to go anywhere. When I currently go to blog.mysite.com, it just takes me to the home page.
I suspect I will have to add a MapRoute assume it will be smart enough to still Ignore the {folder} one.
I think the answer to this question might help you along:
Is it possible to make an ASP.NET MVC route based on a subdomain?