I want to run a certain Asp.NET MVC 4 Controller when an URL -probably irrelevant with the controller name- is entered. For example, if the user opens the address "localhost:3364/abc/def", I want to run the controller with the name, for example, "SugarController". Is it possible, or do I have to start my URL with the word "Sugar"? I know that URL routing can be done by adding some code to Global.asax file in the project. But I don't exactly know how to manage this one.
Thanks in advance.
This should work
routes.MapRoute("Fixed", "abc/def", new { controller = "Sugar", action = "def"});
Related
I am working on MVC and i started learning Umbraco, I didn't get how to bind the umbraco page with mvc controller get method to show the database values. can anyone suggest any url or video?
Thansk...
What you're looking for is Umbraco route hijacking.
You can read about it here.
https://our.umbraco.org/documentation/reference/routing/custom-controllers
It's easiest to demonstrate with an example : let's say you have a Document Type called 'Home'. You can create a custom locally declared controller in your MVC web project called 'HomeController' and ensure that it inherits from Umbraco.Web.Mvc.RenderMvcController and now all pages that are of document type 'Home' will be routed through your custom controller! Pretty easy right :-) OK so let's see how we can extend this concept. In order for you to run some code in your controller you'll need to override the Index Action.
So, basically, you "simply" need to create a controller named after your document type, so for example, a document type with the name "TextPage" would need a controller called "TextPageController". Now, if you read through the documentation, you'll find that your "TextPageController" will need to inherit from the RenderMvcController. Here's an example how to achieve this.
public class TextPageController : RenderMvcController
{
public ActionResult Index()
{
return View("~/Views/TextPage.cshtml");
}
}
This forum link may help you:
https://our.umbraco.org/forum/developers/razor/38242-Umbraco-MVC4111-Surface-controller-using-an-AJAX-form
I am trying to create two MVC applications, and in first application created one view and compiled the application, and in the second application i have added the dll of the first application.
in the routeconfig.cs file added the below line.
"Structure of First application"
Application1
Controller
Home(Actionmethod is Home)
View
Home.cshtml
Application2: references:Application1
_layout.cshtml.
trying to access url: localhost/Home
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "TestApp.Controllers" }
and i am trying to call the view from the first application, but it is not able to find the view from the first application, can any body tell me any other configurations do i need to add to access the view of first application.
Thanks in advance.
Please explain why you have them in two separate mvc applications.
app 1.
controller
view
model
app 2.
controller
view
model
Im not sure if view are compiled into the .dll if they are. Then how are you accessing them?
Also you need to register your search locations for your views... I assume your are using the razor engine.
http://theshravan.net/blog/configure-the-views-search-locations-in-asp-net-mvc/
Can I specify a custom location to "search for views" in ASP.NET MVC?
Basically you need to register the search locations for your views the default is
App
Views
[ViewName]Controller
I have a strong feeling the app which references the .dll [App1]
Has nothing in it to tell it to look in App2 for views as well.
Hope this makes sense ;-)
Try creating Area in your project and create required controller and views instead of accessing view of another project.
I'm trying to develop a multi-tenancy project wherein each client can have their own specific view engines.
Specifically I'd like to compile the views to a DLL (using RazorEngine) and have an individual RazorViewEngine for each client but also provide a fallback to the standard RazorViewEngine if no matching views are found, just as the MVC framework does if you have multiple view engines specified.
I have found I can inject view engines using autofac in the Global.asax of my MVC 4 project using:
ViewEngines.Engines.Clear();
var builder = new ContainerBuilder();
builder.RegisterType<WebFormViewEngine>().As<IViewEngine>();
Now I also want to provide tenant specific overrides as mentioned above which I can do with the following code:
var mtc = new MultitenantContainer(tenantIdStrategy, builder.Build());
mtc.ConfigureTenant("Client1", b => b.RegisterType<RazorViewEngine>().As<IViewEngine>());
DependencyResolver.SetResolver(new AutofacDependencyResolver(mtc));
In this example code I just wanted to see if I could set the WebFormsViewEngine as a fallback and then enable the RazorViewEngine for a specific tenant.
Upon loading and browsing to a non-tenant url, mvc will resolve just the WebFormsViewEngine as expected (through calling the DependencyResolver and in turn Autofac), this works as expected however when I then visit a url that would also include the RazorViewEngine no views are found, even though a valid razor view exists.
Conversely if I stop IISExpress or do something to generate an app pool recycle and visit a tenantable url first both view engines are registered.
From what I can tell MVC caches the list of view engines retrieved after the first call to the MultiServiceResolver.
internal IViewEngine[] CombinedItems
{
get
{
IViewEngine[] combinedItems = _combinedItems;
if (combinedItems == null)
{
combinedItems = MultiServiceResolver.GetCombined<IViewEngine>(Items, _dependencyResolver);
_combinedItems = combinedItems;
}
return combinedItems;
}
}
Is there a way to override this caching or another better way to achieve what I'm trying to get here?
I have two controllers in my MVC4 Project.
One controller is in default controller name called Login.
another controller is in area .area name called HR.inside controller name called AddNewHire.
inside AddNewHire i written AddNewEmployee method.
in Login controller i am having one method .that name is LoginButton
inside LoginButton method i written
return RedirectToAction("AddNewEmployee","AddNewHire",new {area="HR"});
am getting Error Like
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /AddNewHireController/AddNewEmployee
Try using RedirectToRoute instead of RedirectToAction. What you would have to do is to define a route. Have a look at this MSDN link (Walkthrough: Creating an ASP.NET MVC Areas Application Using Multiple Projects
VS2010 w/Ria Services & SL4.
I already have my SL app loaded in the browser.
User clicks around to different view pages and I would like to store their current location in a list of "recently visited pages".
To do that, I need to store the view page + any existing querystring key/values.
Ex: here is a real page in my sl app:
http://localhost:52878/Default.aspx#/DocSearchShell?CM-Origination
I don't care about anything before the # sign - there are a million ways on google to get the http: // localhost:52878/Default.aspx uri from sl.
What I DO want is the "DocSearchShell?CM-Origination" portion of the querystring - that is what I want to capture and store for the "recently visited pages"
How do I do this from sl4 (.NET 4) code-behind?
Thanks!
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// The answer is this:
// e.Uri.OriginalString;
}
You can access the part of query string after "#" symbol via System.Windows.Browser.HtmlPage.Window.CurrentBookmark.
It works from any part of SL application.